-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgg.py
executable file
·79 lines (55 loc) · 2.53 KB
/
vgg.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
# -*- coding: utf-8 -*-
"""vgg.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1soo3bgGFOffTW0idQ4rd-lpe6D2KPYUC
"""
!pip install kaggle
from google.colab import files
uploaded = files.upload()
for fn in uploaded.keys():
print('User uploaded file "{name}" with length {length} bytes'.format(
name=fn, length=len(uploaded[fn])))
# Then move kaggle.json into the folder where the API expects to find it.
!mkdir -p ~/.kaggle/ && mv kaggle.json ~/.kaggle/ && chmod 600 ~/.kaggle/kaggle.json
!kaggle datasets download -d moltean/fruits
!unzip ./fruits.zip
import tensorflow as tf
import keras
from glob import glob
import matplotlib.pyplot as plt
traning_file="./fruits-360-original-size/fruits-360-original-size/Training"
test_file="./fruits-360-original-size/fruits-360-original-size/Test"
validation_file="./fruits-360-original-size/fruits-360-original-size/Validation"
traning_file_image=glob(traning_file+"/*/*.jp*g")
test_file_image=glob(test_file+"/*/*.jp*g")
validation_file_image=glob(validation_file+"/*/*.jp*g")
gen=keras.preprocessing.image.ImageDataGenerator(horizontal_flip=True,vertical_flip=True
,shear_range=0.1,width_shift_range=1.0,
height_shift_range=0.1,
zoom_range=0.1,
preprocessing_function=keras.applications.vgg16.preprocess_input)
training_data=gen.flow_from_directory(traning_file,target_size=(100,100))
test_data=gen.flow_from_directory(test_file,target_size=(100,100))
validation_data=gen.flow_from_directory(validation_file,target_size=(100,100))
vgg=keras.applications.vgg16.VGG16(include_top=False,input_shape=(100,100,3))
for layer in vgg.layers:
layer.trainable=False
x=keras.layers.Flatten()(vgg.output)
x=keras.layers.Dense(24,activation="softmax")(x)
model=keras.Model(vgg.input,x)
model.compile(loss=keras.losses.categorical_crossentropy,metrics=["accuracy"])
history=model.fit(training_data,batch_size=32,epochs=10,
steps_per_epoch=len(traning_file_image)//32,
validation_data=validation_data,
validation_steps=len(validation_file_image)//32
)
plt.plot(history.history["accuracy"],label="accuracy")
plt.plot(history.history["val_accuracy"],label="val_accuracy")
plt.legend()
plt.show()
plt.plot(history.history["loss"],label="loss")
plt.plot(history.history["val_loss"],label="val_loss")
plt.legend()
plt.show()
model.summary()