-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSoilCNNscratch.py
110 lines (82 loc) · 3.45 KB
/
SoilCNNscratch.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
# A soil CNN from scratch.
'''
Will require approx. 200 images for each soil class, 200 * 12 = 2400 images.
'''
# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import BatchNormalization
from keras.models import load_model
from keras import metrics
# Build the model
model = Sequential() # Instantiate the model.
# Step 1 - Convolution
model.add(Convolution2D(32, (3, 3), activation="relu", input_shape=(299, 299, 3)))
# Step 2 - Pooling
model.add(MaxPooling2D(pool_size = (2, 2), strides=2))
# Dropout
model.add(Dropout(0.5))
# Batch norm.
model.add(BatchNormalization())
# Adding a second convolutional layer
model.add(Convolution2D(64, (3, 3), activation="relu"))
# 2nd pooling layer
model.add(MaxPooling2D(pool_size = (2, 2), strides=2))
# Dropout and batch norm.
model.add(Dropout(0.5))
model.add(BatchNormalization())
# Adding a second convolutional layer
model.add(Convolution2D(128, (3, 3), activation="relu"))
# 2nd pooling layer
model.add(MaxPooling2D(pool_size = (2, 2), strides=2))
# Dropout and batch norm.
model.add(Dropout(0.5))
model.add(BatchNormalization())
# Adding a second convolutional layer
model.add(Convolution2D(256, (3, 3), activation="relu"))
# 2nd pooling layer
model.add(MaxPooling2D(pool_size = (2, 2), strides=2))
# Dropout and batch norm.
model.add(Dropout(0.5))
model.add(BatchNormalization())
# Step 3 - Flattening
model.add(Flatten())
# Step 4 - Full connection
model.add(Dense(units=128, activation="relu"))
model.add(Dense(units=256, activation='relu'))
model.add(Dense(units=12, activation="softmax"))
# Compiling the CNN
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = [metrics.hinge])
# Train the model on new data for a few epochs.
from keras.preprocessing.image import ImageDataGenerator
# Create the generators for datasets.
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
rotation_range = 20,
width_shift_range = 30,
height_shift_range = 30)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory(r'soilimages/train',
target_size = (299, 299),
batch_size = 32,
class_mode = 'categorical')
test_set = test_datagen.flow_from_directory(r'soilimages/test',
target_size = (299, 299),
batch_size = 32,
class_mode = 'categorical')
model.fit_generator(training_set, steps_per_epoch=25, epochs=5,
validation_data=test_set, validation_steps=10)
# Save the model
model.save('soilNetPretrained.h5')
# Load the model
model = load_model('soilNetPretrained.h5')
# Get the values from the generator
X_test = list(test_set.next())
# Predict from a batch
y_pred = model.predict((X_test[0]))