-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShip type classification - custom.py
186 lines (124 loc) · 5.32 KB
/
Ship type classification - custom.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
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
# In[1]:
#get_ipython().run_line_magic('reload_ext', 'autoreload')
#get_ipython().run_line_magic('autoreload', '2')
#get_ipython().run_line_magic('matplotlib', 'inline')
import platform
python_v = platform.python_version()
print (python_v)
# In[2]:
import numpy as np
import wget
import zipfile
import os
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras.layers import Dropout, Flatten, Dense
from keras.applications import ResNet50
from keras.models import Model, Sequential
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
from keras.applications.resnet50 import preprocess_input
# ## Load data
# In[3]:
from sklearn.datasets import load_files
from keras.utils import np_utils
import numpy as np
from glob import glob
def download_dataset(url, path='./'):
print(" Downloading dataset from", url)
return wget.download(url)
def unzip(filename, path='./'):
with open(filename, 'rb') as f:
z = zipfile.ZipFile(f)
print(" Unzipping file", filename)
for name in z.namelist():
print(" Extracting file", name)
z.extract(name,path)
def load_dataset(path):
data = load_files(path)
ship_files = np.array(data['filenames'])
ship_targets = np_utils.to_categorical(np.array(data['target']), 133)
return ship_files, ship_targets
train_files, train_targets = load_dataset('shipImages/train')
valid_files, valid_targets = load_dataset('shipImages/valid')
test_files, test_targets = load_dataset('shipImages/test')
ship_names = [item[20:-1] for item in sorted(glob("shipImages/train/*/"))]
# Let's check the dataset
print('There are %d total ship categories.' % len(ship_names))
print('There are %s total ship images.\n' % len(np.hstack([train_files, valid_files, test_files])))
print('There are %d training ship images.' % len(train_files))
print('There are %d validation ship images.' % len(valid_files))
print('There are %d test ship images.'% len(test_files))
# ## Pre-process data
# In[4]:
from keras.preprocessing import image
from tqdm import tqdm
def path_to_tensor(img_path):
# loads RGB image as PIL.Image.Image type
img = image.load_img(img_path, target_size=(224, 224))
# convert PIL.Image.Image type to 3D tensor with shape (224, 224, 3)
x = image.img_to_array(img)
# convert 3D tensor to 4D tensor with shape (1, 224, 224, 3) and return 4D tensor
return np.expand_dims(x, axis=0)
def paths_to_tensor(img_paths):
list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]
return np.vstack(list_of_tensors)
# In[5]:
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
# pre-process the data for Keras
train_tensors = paths_to_tensor(train_files).astype('float32')/255
valid_tensors = paths_to_tensor(valid_files).astype('float32')/255
test_tensors = paths_to_tensor(test_files).astype('float32')/255
# ## Define the network
# In[6]:
from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D
from keras.layers import Dropout, Activation, Dense
from keras.models import Sequential
from keras.layers.normalization import BatchNormalization
model = Sequential()
model.add(Conv2D(16, (3, 3), padding='same', use_bias=False, input_shape=(224, 224, 3)))
model.add(BatchNormalization(axis=3, scale=False))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(4, 4), strides=(4, 4), padding='same'))
model.add(Dropout(0.2))
model.add(Conv2D(32, (3, 3), padding='same', use_bias=False))
model.add(BatchNormalization(axis=3, scale=False))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(4, 4), strides=(4, 4), padding='same'))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), padding='same', use_bias=False))
model.add(BatchNormalization(axis=3, scale=False))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(4, 4), strides=(4, 4), padding='same'))
model.add(Dropout(0.2))
model.add(Conv2D(128, (3, 3), padding='same', use_bias=False))
model.add(BatchNormalization(axis=3, scale=False))
model.add(Activation("relu"))
model.add(Flatten())
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dense(133, activation='softmax'))
model.summary()
# In[7]:
## Compile the network and fit to data
# In[8]:
from keras.callbacks import ModelCheckpoint
EPOCHS = 10
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
checkpointer = ModelCheckpoint(filepath='saved_models/weights.best.from_scratch.hdf5',
verbose=1, save_best_only=True)
model.fit(train_tensors, train_targets,
validation_data=(valid_tensors, valid_targets),
epochs=EPOCHS, batch_size=32, callbacks=[checkpointer], verbose=1)
# ## Load the pre-trained weights (transfer learning)
# In[ ]:
model.load_weights('saved_models/weights.best.from_scratch.hdf5')
# ## Test the model
# In[ ]:
# get index of predicted ship type for each image in test set
ship_type_predictions = [np.argmax(model.predict(np.expand_dims(tensor, axis=0))) for tensor in test_tensors]
# report test accuracy
test_accuracy = 100*np.sum(np.array(ship_type_predictions)==np.argmax(test_targets, axis=1))/len(ship_type_predictions)
print('Test accuracy: %.4f%%' % test_accuracy)