-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA1-T2-c.py
177 lines (126 loc) · 5.18 KB
/
A1-T2-c.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from __future__ import print_function
import keras
from keras.models import Model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Input, Conv2D, MaxPooling2D
from keras import backend as K
import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import pandas as pd
# In[2]:
#Load Data
images = np.load('data/images.npy')
labels = np.load('data/labels.npy')
#Shuffle
indices = np.arange(images.shape[0])
np.random.shuffle(indices)
images = images[indices]
labels = labels[indices]
# input image dimensions
img_rows, img_cols = images.shape[1], images.shape[2]
if K.image_data_format() == 'channels_first':
images = images.reshape(images.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
images = images.reshape(images.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
images = images.astype('float32')
images /= 255
#80/10/10% splits for training/validation and test sets
train_images, temp_images, train_labels, temp_labels = train_test_split(images, labels, test_size=0.2)
val_images, test_images, val_labels, test_labels = train_test_split(temp_images, temp_labels, test_size=0.5)
# In[3]:
#“common sense” accuracy
def common_sense_accuracy(predicted, actual):
pred_minutes = predicted[0]*60+predicted[1]
actual_minutes = actual[0]*60+actual[1]
abs_diff = abs(pred_minutes-actual_minutes)
return min(abs_diff, 12*60-abs_diff)
# In[4]:
def re_category(labels):
# Minutes transformation to sine and cosine (radians: 0 to 2π)
minute_angle = labels[:][1] * (2 * np.pi / 60)
minute_sin = np.sin(minute_angle)
minute_cos = np.cos(minute_angle)
transformed_labels = np.vstack((minute_sin, minute_cos)).T
return transformed_labels
train_h_labels = np.array([h[0] for h in train_labels])
val_h_labels= np.array([h[0] for h in val_labels])
test_h_labels = np.array([h[0] for h in test_labels])
train_min_labels = np.array([re_category(h) for h in train_labels])
val_min_labels= np.array([re_category(h) for h in val_labels])
test_min_labels = np.array([re_category(h) for h in test_labels])
# In[5]:
batch_size = 32
epochs = 30
# Build Model
input_ = Input(shape=(img_rows, img_cols, 1))
x = Conv2D(32, kernel_size=(3, 3),
activation='relu')(input_)
x = Conv2D(64, (3, 3), activation='relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.1)(x)
x = Conv2D(128, (3, 3), activation='relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.1)(x)
x = Conv2D(256, (3, 3), activation='relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.1)(x)
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.1)(x)
x = Dense(128, activation='relu')(x)
x = Dropout(0.1)(x)
# Output
hours_output = Dense(12, activation='softmax', name='hours_output')(x)
minutes_output = Dense(2, activation='tanh', name='minutes_output')(x)
model = Model(inputs=[input_], outputs=[hours_output, minutes_output])
model.compile(loss={'hours_output':keras.losses.sparse_categorical_crossentropy, 'minutes_output': 'mean_squared_error'},
optimizer=keras.optimizers.Adam(learning_rate=1e-4),
metrics={'hours_output': 'accuracy', 'minutes_output': 'mae'})
# Fit Model
history = model.fit(train_images, {'hours_output': train_h_labels, 'minutes_output': train_min_labels},
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(val_images, {'hours_output': val_h_labels, 'minutes_output': val_min_labels}))
df = pd.DataFrame(history.history)
df.plot(figsize=(8, 5))
plt.grid(True)
# plt.gca().set_ylim(0, 1)
plt.show()
# Evaluate Model
score = model.evaluate(val_images, {'hours_output': val_h_labels, 'minutes_output': val_min_labels}, verbose=0)
print('Validation hours_output_loss:', score[2])
print('Validation minutes_output_loss:', score[3])
score_final = model.evaluate(test_images, {'hours_output': test_h_labels, 'minutes_output': test_min_labels}, verbose=0)
print('Test hours_output_loss:', score_final[2])
print('Test minutes_output_loss:', score_final[3])
# In[6]:
def calculate_time_from_predictions(predictions):
minute_sin, minute_cos = predictions[:, 0], predictions[:, 1]
# Calculate angles for minutes
minute_angle = np.arctan2(minute_sin, minute_cos)
# Convert angles back to minute values
minute = ((minute_angle % (2 * np.pi)) * (60 / (2 * np.pi))).astype(int)
return minute
# In[7]:
predictions = model.predict(test_images)
predicted_hours = np.argmax(predictions[0], axis=1)
predicted_minutes = calculate_time_from_predictions(predictions[1])
common_sense_errors = [common_sense_accuracy([predicted_hours[i], predicted_minutes[i]], test_labels[i])
for i in range(len(test_labels))]
print('Average common sense error:', np.mean(common_sense_errors))
# In[8]:
# Plot a sample of clock images
plt.figure(figsize=(12, 12))
for i in range(16):
plt.subplot(4, 4, i + 1)
plt.imshow(test_images[i], cmap='gray')
plt.title(f'Predict: {predicted_hours[i]}:{predicted_minutes[i]:02d},Label: {test_labels[i][0]}:{test_labels[i][1]:02d}')
plt.axis('off')
plt.show()