forked from sihyun0826/theano_artistic_cnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_img.py
35 lines (22 loc) · 997 Bytes
/
preprocess_img.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
import numpy as np
from os import listdir
from os.path import isfile, join
from scipy import misc
import matplotlib.pyplot as plt
img_mean = np.load('pretrained_weights/img_mean.npy')
print 'img_mean shape : ',img_mean.shape
print np.mean(np.mean(img_mean,axis=1),axis=1)
file_list = ['van_gogh_starry_night.jpg', 'kaist_n1.jpg']
for i in file_list:
f = misc.imread(i)
min_dim, max_dim = np.argmin(f.shape[:2]), np.argmax(f.shape[:2])
resize_scale = 227.0/f.shape[min_dim]
f = misc.imresize(f,[int(f.shape[0]*resize_scale),int(f.shape[1]*resize_scale)])
f = f[int((f.shape[0]-227.0)/2):int((f.shape[0]-227.0)/2)+227, int((f.shape[1]-227.0)/2):int((f.shape[1]-227.0)/2)+227, :]
print 'image shape(before) : ',f.shape
plt.imshow(f)
plt.show()
f = np.transpose(f,(2,0,1))
print 'image shape(after) : ',f.shape
preprocessed_img = np.asarray(f,dtype=np.float32)-img_mean[:,16:16+227,16:16+227]
np.save(i[:len(i)-4]+'.npy',preprocessed_img)