-
Notifications
You must be signed in to change notification settings - Fork 1
/
Keras_try.py
160 lines (121 loc) · 4.81 KB
/
Keras_try.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
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D, BatchNormalization
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard
import cv2
from tqdm import tqdm
from sklearn.metrics import fbeta_score
# Params
input_size = 64
input_channels = 3
epochs = 15
batch_size = 128
learning_rate = 0.001
lr_decay = 1e-4
valid_data_size = 5000 # Samples to withhold for validation
model = Sequential()
model.add(BatchNormalization(input_shape=(input_size, input_size, input_channels)))
model.add(Conv2D(32, kernel_size=(2, 2), padding='same', activation='relu'))
model.add(Conv2D(32, kernel_size=(2, 2), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=(2, 2), padding='same', activation='relu'))
model.add(Conv2D(64, kernel_size=(2, 2), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
#model.add(Conv2D(128, kernel_size=(2, 2), padding='same', activation='relu'))
#model.add(Conv2D(128, kernel_size=(2, 2), activation='relu'))
#model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.25))
#model.add(Conv2D(256, kernel_size=(2, 2), padding='same', activation='relu'))
#model.add(Conv2D(256, kernel_size=(2, 2), activation='relu'))
#model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.25))
model.add(Flatten())
#model.add(Dense(512, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(17, activation='sigmoid'))
df_train_data = pd.read_csv('input/train_v2.csv')
flatten = lambda l: [item for sublist in l for item in sublist]
labels = list(set(flatten([l.split(' ') for l in df_train_data['tags'].values])))
label_map = {l: i for i, l in enumerate(labels)}
inv_label_map = {i: l for l, i in label_map.items()}
x_valid = []
y_valid = []
df_valid = df_train_data[(len(df_train_data) - valid_data_size):]
for f, tags in tqdm(df_valid.values, miniters=100):
img = cv2.resize(cv2.imread('input/train-jpg/{}.jpg'.format(f)), (input_size, input_size))
targets = np.zeros(17)
for t in tags.split(' '):
targets[label_map[t]] = 1
x_valid.append(img)
y_valid.append(targets)
y_valid = np.array(y_valid, np.uint8)
x_valid = np.array(x_valid, np.float32)
x_train = []
y_train = []
df_train = df_train_data[:(len(df_train_data) - valid_data_size)]
for f, tags in tqdm(df_train.values, miniters=1000):
img = cv2.resize(cv2.imread('input/train-jpg/{}.jpg'.format(f)), (input_size, input_size))
targets = np.zeros(17)
for t in tags.split(' '):
targets[label_map[t]] = 1
x_train.append(img)
y_train.append(targets)
img = cv2.flip(img, 0) # flip vertically
x_train.append(img)
y_train.append(targets)
img = cv2.flip(img, 1) # flip horizontally
x_train.append(img)
y_train.append(targets)
img = cv2.flip(img, 0) # flip vertically
x_train.append(img)
y_train.append(targets)
y_train = np.array(y_train, np.uint8)
x_train = np.array(x_train, np.float32)
df_test_data = pd.read_csv('input/sample_submission_v2.csv')
x_test = []
for f, tags in tqdm(df_test_data.values, miniters=1000):
img = cv2.resize(cv2.imread('input/test-jpg/{}.jpg'.format(f)), (input_size, input_size))
x_test.append(img)
x_test = np.array(x_test, np.float32)
callbacks = [EarlyStopping(monitor='val_loss',
patience=5,
verbose=0),
TensorBoard(log_dir='logs'),
ModelCheckpoint('weights.h5',
save_best_only=True)]
opt = Adam(lr=learning_rate, decay=lr_decay)
model.compile(loss='binary_crossentropy',
# We NEED binary here, since categorical_crossentropy l1 norms the output before calculating loss.
optimizer=opt,
metrics=['accuracy'])
model.fit(x_train,
y_train,
batch_size=batch_size,
epochs=epochs,
verbose=2,
callbacks=callbacks,
validation_data=(x_valid, y_valid))
p_valid = model.predict(x_valid, batch_size=batch_size)
print(fbeta_score(y_valid, np.array(p_valid) > 0.2, beta=2, average='samples'))
y_test = []
p_test = model.predict(x_test, batch_size=batch_size, verbose=2)
y_test.append(p_test)
result = np.array(y_test[0])
result = pd.DataFrame(result, columns=labels)
preds = []
for i in tqdm(range(result.shape[0]), miniters=1000):
a = result.ix[[i]]
a = a.apply(lambda x: x > 0.2, axis=1)
a = a.transpose()
a = a.loc[a[i] == True]
' '.join(list(a.index))
preds.append(' '.join(list(a.index)))
df_test_data['tags'] = preds
df_test_data.to_csv('submission.csv', index=False)
# 0.918