###前提・実現したいこと
chainerをGPU環境で動かして、DCGANのプログラムを実行しようとしたときに「read image error occured
画像名.jpg」とエラーが発生しました。
エラーの解決方法を教えていただけないでしょうか
OS Windows10
CUDA 8.0
GPU GTX 950x
python 3.5 64bit
###発生している問題・エラーメッセージ
read image error occured ~.jpg
###該当のソースコード
python
1import pickle 2import numpy as np 3from PIL import Image 4import os 5import io as cStringIO 6import math 7import pylab 8 9 10import chainer 11from chainer import computational_graph 12from chainer import cuda 13from chainer import optimizers 14from chainer import serializers 15from chainer import Variable 16from chainer.utils import type_check 17from chainer import function 18 19import chainer.functions as F 20import chainer.links as L 21 22 23import numpy 24import io 25 26image_dir = 'C:\images' 27out_image_dir = 'C:\out_images' 28out_model_dir = 'C:\out_models' 29 30 31nz = 100 # # of dim for Z 32batchsize=100 33n_epoch=10000 34n_train=200000 35image_save_interval = 50000 36 37# read all images 38 39fs = os.listdir(image_dir) 40print (len(fs)) 41dataset = [] 42for fn in fs: 43 f = open('%s/%s'%(image_dir,fn), 'rb') 44 img_bin = f.read() 45 dataset.append(img_bin) 46 f.close() 47print (len(dataset)) 48 49class ELU(function.Function): 50 51 """Exponential Linear Unit.""" 52 # https://github.com/muupan/chainer-elu 53 54 def __init__(self, alpha=1.0): 55 self.alpha = numpy.float32(alpha) 56 57 def check_type_forward(self, in_types): 58 type_check.expect(in_types.size() == 1) 59 x_type, = in_types 60 61 type_check.expect( 62 x_type.dtype == numpy.float32, 63 ) 64 65 def forward_cpu(self, x): 66 y = x[0].copy() 67 neg_indices = x[0] < 0 68 y[neg_indices] = self.alpha * (numpy.exp(y[neg_indices]) - 1) 69 return y, 70 71 def forward_gpu(self, x): 72 y = cuda.elementwise( 73 'T x, T alpha', 'T y', 74 'y = x >= 0 ? x : alpha * (exp(x) - 1)', 'elu_fwd')( 75 x[0], self.alpha) 76 return y, 77 78 def backward_cpu(self, x, gy): 79 gx = gy[0].copy() 80 neg_indices = x[0] < 0 81 gx[neg_indices] *= self.alpha * numpy.exp(x[0][neg_indices]) 82 return gx, 83 84 def backward_gpu(self, x, gy): 85 gx = cuda.elementwise( 86 'T x, T gy, T alpha', 'T gx', 87 'gx = x >= 0 ? gy : gy * alpha * exp(x)', 'elu_bwd')( 88 x[0], gy[0], self.alpha) 89 return gx, 90 91 92def elu(x, alpha=1.0): 93 """Exponential Linear Unit function.""" 94 # https://github.com/muupan/chainer-elu 95 return ELU(alpha=alpha)(x) 96 97 98 99 100class Generator(chainer.Chain): 101 def __init__(self): 102 super(Generator, self).__init__( 103 l0z = L.Linear(nz, 6*6*512, wscale=0.02*math.sqrt(nz)), 104 dc1 = L.Deconvolution2D(512, 256, 4, stride=2, pad=1, wscale=0.02*math.sqrt(4*4*512)), 105 dc2 = L.Deconvolution2D(256, 128, 4, stride=2, pad=1, wscale=0.02*math.sqrt(4*4*256)), 106 dc3 = L.Deconvolution2D(128, 64, 4, stride=2, pad=1, wscale=0.02*math.sqrt(4*4*128)), 107 dc4 = L.Deconvolution2D(64, 3, 4, stride=2, pad=1, wscale=0.02*math.sqrt(4*4*64)), 108 bn0l = L.BatchNormalization(6*6*512), 109 bn0 = L.BatchNormalization(512), 110 bn1 = L.BatchNormalization(256), 111 bn2 = L.BatchNormalization(128), 112 bn3 = L.BatchNormalization(64), 113 ) 114 115 def __call__(self, z, test=False): 116 h = F.reshape(F.relu(self.bn0l(self.l0z(z), test=test)), (z.data.shape[0], 512, 6, 6)) 117 h = F.relu(self.bn1(self.dc1(h), test=test)) 118 h = F.relu(self.bn2(self.dc2(h), test=test)) 119 h = F.relu(self.bn3(self.dc3(h), test=test)) 120 x = (self.dc4(h)) 121 return x 122 123 124 125class Discriminator(chainer.Chain): 126 def __init__(self): 127 super(Discriminator, self).__init__( 128 c0 = L.Convolution2D(3, 64, 4, stride=2, pad=1, wscale=0.02*math.sqrt(4*4*3)), 129 c1 = L.Convolution2D(64, 128, 4, stride=2, pad=1, wscale=0.02*math.sqrt(4*4*64)), 130 c2 = L.Convolution2D(128, 256, 4, stride=2, pad=1, wscale=0.02*math.sqrt(4*4*128)), 131 c3 = L.Convolution2D(256, 512, 4, stride=2, pad=1, wscale=0.02*math.sqrt(4*4*256)), 132 l4l = L.Linear(6*6*512, 2, wscale=0.02*math.sqrt(6*6*512)), 133 bn0 = L.BatchNormalization(64), 134 bn1 = L.BatchNormalization(128), 135 bn2 = L.BatchNormalization(256), 136 bn3 = L.BatchNormalization(512), 137 ) 138 139 def __call__(self, x, test=False): 140 h = elu(self.c0(x)) # no bn because images from generator will katayotteru? 141 h = elu(self.bn1(self.c1(h), test=test)) 142 h = elu(self.bn2(self.c2(h), test=test)) 143 h = elu(self.bn3(self.c3(h), test=test)) 144 l = self.l4l(h) 145 return l 146 147 148 149 150def clip_img(x): 151 return np.float32(-1 if x<-1 else (1 if x>1 else x)) 152 153 154def train_dcgan_labeled(gen, dis, epoch0=0): 155 o_gen = optimizers.Adam(alpha=0.0002, beta1=0.5) 156 o_dis = optimizers.Adam(alpha=0.0002, beta1=0.5) 157 o_gen.setup(gen) 158 o_dis.setup(dis) 159 o_gen.add_hook(chainer.optimizer.WeightDecay(0.00001)) 160 o_dis.add_hook(chainer.optimizer.WeightDecay(0.00001)) 161 162 zvis = (xp.random.uniform(-1, 1, (100, nz), dtype=np.float32)) 163 164 for epoch in range(epoch0,n_epoch): 165 perm = np.random.permutation(n_train) 166 sum_l_dis = np.float32(0) 167 sum_l_gen = np.float32(0) 168 169 for i in range(0, n_train, batchsize): 170 # discriminator 171 # 0: from dataset 172 # 1: from noise 173 174 print ("load image start ", i) 175 x2 = np.zeros((batchsize, 3, 96, 96), dtype=np.float32) 176 for j in range(batchsize): 177 try: 178 rnd = np.random.randint(len(dataset)) 179 rnd2 = np.random.randint(2) 180 181 img = np.asarray(Image.open(cStringIO(dataset[rnd])).convert('RGB')).astype(np.float32).transpose(2, 0, 1) 182 if rnd2==0: 183 x2[j,:,:,:] = (img[:,:,::-1]-128.0)/128.0 184 else: 185 x2[j,:,:,:] = (img[:,:,:]-128.0)/128.0 186 except: 187 print ('read image error occured', fs[rnd]) 188 #print "load image done" 189 190 # train generator 191 z = Variable(xp.random.uniform(-1, 1, (batchsize, nz), dtype=np.float32)) 192 x = gen(z) 193 yl = dis(x) 194 L_gen = F.softmax_cross_entropy(yl, Variable(xp.zeros(batchsize, dtype=np.int32))) 195 L_dis = F.softmax_cross_entropy(yl, Variable(xp.ones(batchsize, dtype=np.int32))) 196 197 # train discriminator 198 199 x2 = Variable(cuda.to_gpu(x2)) 200 yl2 = dis(x2) 201 L_dis += F.softmax_cross_entropy(yl2, Variable(xp.zeros(batchsize, dtype=np.int32))) 202 203 #print "forward done" 204 205 o_gen.zero_grads() 206 L_gen.backward() 207 o_gen.update() 208 209 o_dis.zero_grads() 210 L_dis.backward() 211 o_dis.update() 212 213 sum_l_gen += L_gen.data.get() 214 sum_l_dis += L_dis.data.get() 215 216 #print "backward done" 217 218 if i%image_save_interval==0: 219 pylab.rcParams['figure.figsize'] = (16.0,16.0) 220 pylab.clf() 221 vissize = 100 222 z = zvis 223 z[50:,:] = (xp.random.uniform(-1, 1, (50, nz), dtype=np.float32)) 224 z = Variable(z) 225 x = gen(z, test=True) 226 x = x.data.get() 227 for i_ in range(100): 228 tmp = ((np.vectorize(clip_img)(x[i_,:,:,:])+1)/2).transpose(1,2,0) 229 pylab.subplot(10,10,i_+1) 230 pylab.imshow(tmp) 231 pylab.axis('off') 232 pylab.savefig('%s/vis_%d_%d.png'%(out_image_dir, epoch,i)) 233 234 serializers.save_hdf5("%s/dcgan_model_dis_%d.h5"%(out_model_dir, epoch),dis) 235 serializers.save_hdf5("%s/dcgan_model_gen_%d.h5"%(out_model_dir, epoch),gen) 236 serializers.save_hdf5("%s/dcgan_state_dis_%d.h5"%(out_model_dir, epoch),o_dis) 237 serializers.save_hdf5("%s/dcgan_state_gen_%d.h5"%(out_model_dir, epoch),o_gen) 238 print ('epoch end', epoch, sum_l_gen/n_train, sum_l_dis/n_train) 239 240 241 242xp = cuda.cupy 243cuda.get_device(0).use() 244 245gen = Generator() 246dis = Discriminator() 247gen.to_gpu() 248dis.to_gpu() 249 250 251try: 252 os.mkdir(out_image_dir) 253 os.mkdir(out_model_dir) 254except: 255 pass 256 257train_dcgan_labeled(gen, dis)
###試したこと
画像サイズに問題があると考え、96×96を用意できているか確認しました
また、read image error のソースコードの部分を見て、ネット上で調べてみましたが、わかりませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/12 02:20
2017/05/12 03:48 編集
2017/05/12 17:00
2017/05/13 00:52
2017/05/13 03:25