-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRice_Disease_Classifier.py
153 lines (104 loc) · 4.62 KB
/
Rice_Disease_Classifier.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
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
from __future__ import absolute_import, print_function,division, unicode_literals
import os.path
import glob
import shutil
import tensorflow as tf
assert tf.__version__.startswith('2')
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Conv2D,Flatten,MaxPooling2D,Dropout,Dense,Activation
from keras import regularizers
import keras
import numpy as np
import matplotlib.pyplot as plt
import pathlib
print ('successful')
gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(gpus[0], True)
print(tf.__version__)
BATCH_SIZE = 8
IMG_HEIGHT = 224
IMG_WIDTH = 224
data_dir =r"D:\qianSpace\NongYexinxichulizongheshijian\WorkSpace\archive\rice_leaf_diseases"
data_dir = pathlib.Path(data_dir)
CLASS_NAMES = np.array(['Leaf Blight','Brown Spot','Leaf Smut'])
print('Class Names: ', CLASS_NAMES)
train_path = r'D:\qianSpace\NongYexinxichulizongheshijian\WorkSpace\archive\rice_leaf_diseases'
test_path = r'D:\qianSpace\NongYexinxichulizongheshijian\WorkSpace\archive\rice_leaf_diseases'
image_train_gen = ImageDataGenerator(rescale=1./255,
zoom_range=0.50,
rotation_range=45,
horizontal_flip=True,
width_shift_range=0.15,
height_shift_range=0.15)
train_data_gen = image_train_gen.flow_from_directory(train_path,
shuffle=True,
batch_size=BATCH_SIZE,
target_size=(IMG_HEIGHT,IMG_WIDTH),
class_mode='sparse')
img_val_gen = ImageDataGenerator(rescale=1./255)
val_data_gen = img_val_gen.flow_from_directory(test_path,
batch_size=BATCH_SIZE,
target_size=(IMG_HEIGHT,IMG_WIDTH),
class_mode='sparse')
def plotImages(image_arr):
fig,axes = plt.subplots(1, 5, figsize=(20,20))
axes = axes.flatten()
for img,ax in zip(image_arr,axes):
ax.imshow(img)
plt.tight_layout()
plt.show()
# Plot a few training images
img_array = [train_data_gen[0][0][0] for i in range(5)]
plotImages(img_array)
# plot a few val images
img_array = [val_data_gen[0][0][0] for i in range(5)]
plotImages(img_array)
# Model building
#Instatiating A convnet
model = Sequential()
model.add(Conv2D(16, (3,3), input_shape=(224,224,3), activation="relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Conv2D(32, (3,3), activation="relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Conv2D(64, (3,3), activation="relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Flatten())
model.add(Dropout(0.2))
model.add(Dense(128,activation="relu"))
model.add(Dropout(0.2))
model.add(Dense(3, activation="softmax"))
model.compile(
optimizer = "adam",
loss = "sparse_categorical_crossentropy",
metrics = ['accuracy']
)
model.summary()
EPOCHS=50
history = model.fit_generator(train_data_gen, epochs=EPOCHS, validation_data=val_data_gen)
# Plot training and validation graphs
acc = history.history['accuracy']
val_accuracy = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(EPOCHS)
plt.figure(figsize=(12,12))
plt.subplot(1,2,1)
plt.plot(epochs_range,acc,label='Training Accuracy')
plt.plot(epochs_range,val_accuracy,label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1,2,2)
plt.plot(epochs_range,loss,label='Training Loss')
plt.plot(epochs_range,val_loss,label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
#++++++++++++++++++++模型保存测试+++++++++++++++++++++++
# 模型保存,注意:仅仅是多了一个save_format的参数而已
# 注意:这里的'path_to_saved_model'不再是模型名称,仅仅是一个文件夹,模型会保存在这个文件夹之下
#model.save('path_to_saved_model', save_format='tf')
model.save('path_to_saved_model_h5.h5', save_format='h5')
# 加载模型,通过指定存放模型的文件夹来加载
#new_model = keras.models.load_model('path_to_saved_model')