-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathart_rfc.py
194 lines (152 loc) · 6.64 KB
/
art_rfc.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
186
187
188
189
190
191
192
193
194
'''
make_submission() generates predictions for the
Kaggle Painter by Numbers competition using simple
features (image size, aspect ratio and bits/pixel^2)
Note: Make sure you're in the directory containing your data files before
running this script.
author: Small Yellow Duck
https://github.com/small-yellow-duck/kaggle_art
'''
import os
import numpy as np
import pandas as pd
# PIL available via Anaconda install
from PIL import Image
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score
def get_image_info(test_info, dir):
if dir == 'test':
images = list(set(list(test_info.image1.unique()) +
list(test_info.image2.unique())))
info = pd.DataFrame(np.array(images).reshape(
(-1, 1)), columns=['filename'])
else:
info = test_info
info['pixelsx'] = np.nan
info['pixelsy'] = np.nan
info['size_bytes'] = np.nan
for i in info.index.values:
try:
im = Image.open(dir + '/' + info.loc[i, 'filename'])
info.loc[i, 'pixelsx'], info.loc[i, 'pixelsy'] = im.size
# im = cv2.imread(dir+'/'+info.loc[i, 'new_filename'])
# info.loc[i, 'pixelsx'], info.loc[i, 'pixelsy'] = im.shape[0:2]
info.loc[i, 'size_bytes'] = os.path.getsize(
dir + '/' + info.loc[i, 'filename'])
except:
print dir + '/' + info.loc[i, 'filename']
return info.rename(columns={'filename': 'new_filename'})
def make_pairs(train_info):
artists = train_info.artist.unique()
n = train_info.groupby('artist').size()
n = (2 * n**2).sum()
pairs_cols = ['artist1', 'image1', 'artist2', 'image2']
t = pd.DataFrame(np.zeros((n, 4)), columns=pairs_cols)
i = 0
j = 0
for m in artists:
a = train_info[train_info.artist == m][
['artist', 'new_filename']].values
use = train_info[train_info.artist != m].index.values
np.random.shuffle(use)
nm = np.min(
[a.shape[0]**2, train_info[train_info.artist != m].shape[0]])
use = use[0:nm]
b = train_info[train_info.artist != m][
['artist', 'new_filename']].ix[use, :].values
a2 = pd.DataFrame(np.concatenate([np.repeat(a[:, 0], a.shape[0]).reshape((-1, 1)),
np.repeat(a[:, 1], a.shape[0]).reshape((-1, 1)),
np.tile(a, (a.shape[0], 1))], axis=1),
columns=pairs_cols)
a2 = a2.loc[0:nm, :]
b2 = pd.DataFrame(np.concatenate([np.tile(a, (a.shape[0], 1))[0:b.shape[0], :], b],
axis=1), columns=pairs_cols)
# print j, i, a2.shape[0], b2.shape[0]
# print b2
t.iloc[i:i + a2.shape[0], :] = a2.values
t.iloc[i + a2.shape[0]:i + a2.shape[0] + b2.shape[0], :] = b2.values
i += a2.shape[0] + b2.shape[0]
j += 1
t = t[~t.image2.isin([np.nan, 0])]
return t[t.image1 > t.image2]
def prep_data(input, split):
info = input[0]
data = input[1]
if split == 'cv':
artists = info.artist.unique()
np.random.shuffle(artists)
info = get_image_info(info, 'train')
info['bytes_per_pixel'] = 1.0 * info['size_bytes'] / \
(info['pixelsx'] * info['pixelsy'])
info['aspect_ratio'] = 1.0 * info['pixelsx'] / info['pixelsy']
train_artists = artists[0:int(0.8 * len(artists))]
test_artists = artists[int(0.8 * len(artists)):]
train = make_pairs(info[info.artist.isin(train_artists)])
test = make_pairs(info[info.artist.isin(test_artists)])
train['in_train'] = True
test['in_train'] = False
data = train.append(test)
data['sameArtist'] = data['artist1'] == data['artist2']
if split == 'test':
info = get_image_info(data, 'test')
pixelsxy = info['pixelsx'] * info['pixelsy']
info['bytes_per_pixel'] = 1.0 * info['size_bytes'] / pixelsxy
info['aspect_ratio'] = 1.0 * info['pixelsx'] / info['pixelsy']
data['in_train'] = False
if 'artist1' in data.columns:
data['sameArtist'] = data['artist1'] == data['artist2']
columns = ['new_filename', 'pixelsx', 'pixelsy', 'size_bytes',
'bytes_per_pixel', 'aspect_ratio']
data2 = pd.merge(data, info[columns], how='left', left_on='image1',
right_on='new_filename')
data2.drop('new_filename', 1, inplace=True)
data2 = pd.merge(data2, info[columns], how='left', left_on='image2',
right_on='new_filename')
data2.drop('new_filename', 1, inplace=True)
columns2 = ['pixelsx_x', 'pixelsy_x', 'size_bytes_x', 'bytes_per_pixel_x',
'aspect_ratio_x', 'pixelsx_y', 'pixelsy_y', 'size_bytes_y',
'bytes_per_pixel_y', 'aspect_ratio_y']
# As long as "data.in_train" is an np.array this should work:
x_train = data2[data2.in_train][columns2].values
x_test = data2[~data2.in_train][columns2].values
if 'artist1' in data.columns:
y_train = data2[data2.in_train]['sameArtist'].values
y_test = data2[~data2.in_train]['sameArtist'].values
else:
y_test = None
if split == 'cv':
return x_train, y_train, x_test, y_test
if split == 'test':
return x_test, y_test
def train_classifier(x_train, y_train, x_cv, y_cv):
clf = RandomForestClassifier(n_estimators=100)
print 'starting fit'
# excluding the patient_id column from the fit and prediction (patient_id?)
clf.fit(x_train[::5], y_train[::5])
print 'starting pred'
y_pred = np.zeros(x_cv.shape[0])
for i in xrange(4):
y_pred[i::4] = clf.predict_proba(x_cv[i::4])[:, 1]
if y_cv is not None:
print roc_auc_score(y_cv, y_pred)
return y_pred, clf
def make_submission():
"""
Loads/preps data; trains classifier; and generates appropriate submission
"""
train_info = pd.read_csv('train_info.csv')
submission_info = pd.read_csv('submission_info.csv')
print 'prepping training and cv data'
x_train, y_train, x_cv, y_cv = prep_data([train_info, None], 'cv')
print 'prepping test data'
x_test, y_test = prep_data([None, submission_info], 'test')
print 'starting classifier'
y_pred, clf = train_classifier(x_train, y_train, x_test, y_test)
submission = submission_info[['index']]
submission['sameArtist'] = y_pred
submission.to_csv('submission.csv', index=False)
return
# t = make_pairs(train_info)
# image_info_test = get_image_info(test_info, 'test')
# x_train, y_train, x_cv, y_cv = prep_data([train_info, None], 'cv')
# x_test, y_test = prep_data([None, submission_info], 'test')