質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Q&A

解決済

2回答

2359閲覧

DCGAN:read image error occured の解決

KeiKiha

総合スコア11

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

0グッド

0クリップ

投稿2017/05/11 09:25

###前提・実現したいこと
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/ツール等のバージョンなど)
より詳細な情報

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

コード全体は確認していませんが、少なくともパス文字列に問題があると思います。

たとえばimage_dir = 'C:\images'にて、パス文字列に\が含まれているので、エスケープするimage_dir = 'C:\\images'かraw文字列image_dir = r'C:\images'として指定してみてください。

参考:Windowsのパスがうまく指定\表示できない問題 in Python

投稿2017/05/12 00:21

can110

総合スコア38266

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KeiKiha

2017/05/12 02:20

返信が遅れすみません。 image_dir = 'C:\images'をimage_dir = 'C:\\images'に書き換えるという認識で合っていますでしょうか。 書き換えてみたところ、同じようにread image error と出てきました。 自分でもどこが悪いかを考えているのですが、print文を間に挟みながらどこまでプログラムが正常に進んでいるかを確認していったところ、img = np.asarray(Image.open(cStringIO(dataset[rnd])).convert('RGB')).astype(np.float32).transpose(2, 0, 1)  の部分でerrorになっているように感じられました。 この部分で間違ってたりはしていないでしょうか。
can110

2017/05/12 03:48 編集

まずは Image.open(cStringIO(dataset[rnd])).convert('RGB')).astype(np.float32).transpose(2, 0, 1) での関数を連続している部分を複数行にばらして、どこでエラーが発生しているか確認してみてはいかがでしょうか? ひとつ気になるのは、dataset[rnd]は画像バイナリデータがそのまま入っていますよね。 であればImage.open()の第一引数にはたしかファイルパスを渡すべきなので、おかしいような気がします。
KeiKiha

2017/05/12 17:00

すみません、いろいろ格闘していたら遅くなりました。 python初学者で関数の意味をあまり理解しきれていないのですが、このような分け方でよかったでしょうか。 img = Image.open(cStringIO(dataset[rnd]) ) print ("1") img1 = np.asarray(img ) img2 = img1.convert('RGB') img3 = img2.astype(np.float32) img4 = img3.transpose(2, 0, 1) この各分の間にprintで文字を表示させるように書き、実行したところ、一文目img = Image.open(cStringIO(dataset[rnd]))に対して'module' object is not callableとエラーが出ました。 僕のコードはDCGANのプログラムをpython2.7で書かれていたこの方のコードを参考にpython3.5で使えるように書き直している途中のもので、この方が画像バイナリデータをそのままImage.openで開こうとしていたため参考にしていました。 https://github.com/mattya/chainer-DCGAN Image.openに試しにimage_dir = 'C:\images'のimage_dirを入れてみましたがエラーを出されました。
can110

2017/05/13 00:52

Image.openはバイナリストリーム渡せるようですね。失礼しました。では import io img_io = io.StringIO(dataset[rnd]) # またはio.BytesIOでもよいかも Image.open(img_io) でどうでしょうか?
KeiKiha

2017/05/13 03:25

io.BytesIO でプログラムが動き始めました!ありがとうございます。 まだ計算途中なのでうまく動いているかはわかりませんが今のところは正しく動いていそうです。 また、自分で考えてもわからなかった場合はぜひよろしくお願いいたします。 本当にありがとうございました。
guest

0

いちど、read image error の周りのtry except ブロックをはずしてみてください。

投稿2017/05/11 22:59

MasashiKimura

総合スコア1150

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KeiKiha

2017/05/12 02:28 編集

回答ありがとうございます。 返信が遅れすみません。 for j in range(batchsize): rnd = np.random.randint(len(dataset)) rnd2 = np.random.randint(2) img = np.asarray(Image.open(cStringIO(dataset[rnd])).convert('RGB')).astype(np.float32).transpose(2, 0, 1) if rnd2==0: x2[j,:,:,:] = (img[:,:,::-1]-128.0)/128.0 else: x2[j,:,:,:] = (img[:,:,:]-128.0)/128.0 プログラムをこのようにしたところ、 img = np.asarray(Image.open(cStringIO(dataset[rnd])).convert('RGB')).astype(np.float32).transpose(2, 0, 1)の部分に対して'module' object is not callable というエラーが出てきました。 このエラーメッセージを調べてみましたが、よくわかりませんでした。 教えていただけないでしょうか。
MasashiKimura

2017/05/12 22:43

import io as cStringIO を import cStringIO に変えてみてください
KeiKiha

2017/05/13 03:30

もう一人の回答者の方とKimuraさんの答えを参考にしながらプログラムを書き換えることでプログラムが動き始めました。 もしまたわからない時があったら是非お願いいたします。 本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問