-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectkeempat_rere.py
More file actions
185 lines (145 loc) · 4.71 KB
/
projectkeempat_rere.py
File metadata and controls
185 lines (145 loc) · 4.71 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
"""projectkeempat_rere.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1rI_mgNeuYPNlDkMt2zi9lZl4gNyPihLX
"""
# install kaggle package
!pip install -q kaggle
# upload kaggle.json
from google.colab import files
files.upload()
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
!chmod 600 ~/.kaggle/kaggle.json
!ls ~/.kaggle
!kaggle datasets download -d alessiocorrado99/animals10
# unzip
!mkdir animals
!unzip -qq animals10.zip -d animals
!ls animals
!ls animals/animals10/raw-img/
import os
animals = os.path.join('/content/animals/animals10/raw-img/')
print(os.listdir(animals))
import shutil
ignore_animals = ['squirrel', 'cat', 'butterfly', 'elephant', 'sheep', 'cow', ]
for x in ignore_animals:
path = os.path.join(animals, x)
shutil.rmtree(path)
list_animals = os.listdir(animals)
print(list_animals)
from PIL import Image
total = 0
for x in list_animals:
dir = os.path.join(animals, x)
y = len(os.listdir(dir))
print(x+':', y)
total = total + y
img_name = os.listdir(dir)
for z in range(4):
img_path = os.path.join(dir, img_name[z])
img = Image.open(img_path)
print('-',img.size)
print('---------------')
print('\nTotal :', total)
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 2, figsize=(15,15))
fig.suptitle("Randomly displays images.", fontsize=24)
animals_sorted = sorted(list_animals)
animals_id = 0
for i in range(2):
for j in range(2):
try:
animals_selected = animals_sorted[animals_id]
animals_id += 1
except:
break
if animals_selected == '.TEMP':
continue
animals_selected_images = os.listdir(os.path.join(animals, animals_selected))
animals_selected_random = np.random.choice(animals_selected_images)
img = plt.imread(os.path.join(animals, animals_selected, animals_selected_random))
ax[i][j].imshow(img)
ax[i][j].set_title(animals_selected, pad=10, fontsize=22)
plt.setp(ax, xticks=[],yticks=[])
plt.show
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
rescale=1/255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
validation_split=0.2
)
batch_size = 256
data_train = train_datagen.flow_from_directory(
animals,
target_size=(150, 150),
batch_size=batch_size,
class_mode='categorical',
subset='training')
data_val = train_datagen.flow_from_directory(
animals,
target_size=(150, 150),
batch_size=batch_size,
class_mode='categorical',
subset='validation')
import tensorflow as tf
# from tensorflow.keras import applications, optimizers
tf.device('/device:GPU:0')
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(150, 150, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(4, activation='softmax')
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics = ['accuracy'])
model.summary()
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy')>0.92 and logs.get('val_accuracy')>0.92):
print("\nAccuracy above 92%, finish training!")
self.model.stop_training = True
callbacks = myCallback()
history = model.fit(data_train,
epochs = 64,
steps_per_epoch = data_train.samples // batch_size,
validation_data = data_val,
validation_steps = data_val.samples // batch_size,
verbose = 2,
callbacks = [callbacks])
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with tf.io.gfile.GFile('model.tflite', 'wb') as f:
f.write(tflite_model)
!ls -la | grep 'model'