-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifferent_potato.py
More file actions
103 lines (83 loc) · 2.91 KB
/
different_potato.py
File metadata and controls
103 lines (83 loc) · 2.91 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
from __future__ import print_function
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from random import shuffle
import numpy as np
import os
import json
# Trying out Keras. This is a pretty straightforward conv net
batch_size = 300
num_classes = 1
epochs = 300
data_augmentation = False
num_predictions = 20
save_dir = os.path.join(os.getcwd(), 'saved_models')
model_name = 'trained_model.h5'
cv_size = 160
with open('train_processed_cropped.json') as f:
print('reading', f)
dataset = json.load(f)
print('dataset size', len(dataset))
shuffle(dataset)
x_train = np.empty((len(dataset) - cv_size, 50, 50, 3))
y_train = np.empty((len(dataset) - cv_size))
x_test = np.empty((cv_size, 50, 50, 3))
y_test = np.empty((cv_size))
i = 0
for item in dataset[0:-cv_size]:
band1 = np.asarray(item['band_1']).reshape((50, 50))
band2 = np.asarray(item['band_2']).reshape((50, 50))
band3 = np.asarray(item['band_nabla']).reshape((50, 50))
x_train[i] = np.dstack((band1, band2, band3))
y_train[i] = item['is_iceberg'] == 1 # np.array((item['is_iceberg'] == 1, item['is_iceberg'] == 0))
i += 1
i = 0
for item in dataset[len(dataset) - cv_size:]:
band1 = np.asarray(item['band_1']).reshape((50, 50))
band2 = np.asarray(item['band_2']).reshape((50, 50))
band3 = np.asarray(item['band_nabla']).reshape((50, 50))
x_test[i] = np.dstack((band1, band2, band3))
y_test[i] = item['is_iceberg'] == 1 # np.array((item['is_iceberg'] == 1, item['is_iceberg'] == 0))
i += 1
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
print(y_train)
print(np.sum(y_test))
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', input_shape=x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
model.compile(loss='mean_squared_error', optimizer=opt, metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(x_test, y_test),
shuffle=True)
print(model.predict(x_test))
# Save model and weights
model.save(model_name)
print('Saved trained model at %s ' % model_name)
# Score trained model.
scores = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])