-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_model.py
88 lines (71 loc) · 2.73 KB
/
example_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
"""example_model.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1x3VLaKxa-zwfX287HRw4xriduBIgcMHw
"""
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers,models
from tensorflow.keras.models import Sequential
import matplotlib.pyplot as plt
data_train_path='/content/drive/MyDrive/mint/mint/train'
data_test_path='/content/drive/MyDrive/mint/mint/test'
img_width=180
img_height=180
data_train=tf.keras.utils.image_dataset_from_directory(data_train_path,image_size=(img_width,img_height),shuffle=True,batch_size=32,
validation_split=False)
data_cat=data_train.class_names
print(data_cat)
data_test=tf.keras.utils.image_dataset_from_directory(data_test_path,image_size=(img_width,img_height),shuffle=True,batch_size=32,
validation_split=False)
plt.figure(figsize=(10,10))
for image,labels in data_train.take(1):
for i in range(9):
plt.subplot(3,3,i+1)
plt.imshow(image[i].numpy().astype('uint8'))
plt.title(data_cat[labels[i]])
plt.axis('off')
#BUILDING MODEL USING SEQUENTIAL
model=Sequential([
layers.Rescaling(1./255),
layers.Conv2D(16,3,padding='same',activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32,3,padding='same',activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64,3,padding='same',activation='relu'),
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dropout(0.25),
layers.Dense(128),
layers.Dense(len(data_cat))
])
model.compile(optimizer='adam',loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])
history=model.fit(data_train,epochs=15,batch_size=32,verbose=1)
epochs_range=range(15)
plt.figure(figsize=(8,8))
plt.subplot(1,2,2)
plt.plot(epochs_range,history.history['accuracy'],label='Training Accuracy')
plt.title('Training Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.show()
epochs_range=range(15)
plt.figure(figsize=(8,8))
plt.subplot(1,2,2)
plt.scatter(epochs_range,history.history['accuracy'],label='Training Accuracy')
plt.title('Training Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.show()
image_path='/content/drive/MyDrive/mint/mint/test/good/pill_mint_good_037.png'
image=tf.keras.utils.load_img(image_path,target_size=(img_width,img_height))
img_arr=tf.keras.utils.img_to_array(image)
img_bat=tf.expand_dims(img_arr,0)
predict=model.predict(img_bat)
score=tf.nn.softmax(predict)
print('image is {} with accuracy of {:0.2f}'.format(data_cat[np.argmax(score)], np.max(score)*100))
model.save('pill.keras')