突然TensorflowでAIに取り組むことになりまして
画像の読み込みでエラーが出て困っています。
以下の処理を実行するとload_imageの最後でエラーになります。
lang
1from __future__ import print_function, division 2import numpy as np 3import os, re 4import argparse 5from PIL import Image 6 7from chainer import cuda, Variable, optimizers, serializers 8from net import * 9 10def load_image(path, size): 11 image = Image.open(path).convert('RGB') 12 w,h = image.size 13 if w < h: 14 if w < size: 15 image = image.resize((size, size*h//w)) 16 w, h = image.size 17 else: 18 if h < size: 19 image = image.resize((size*w//h, size)) 20 w, h = image.size 21 image = image.crop(((w-size)*0.5, (h-size)*0.5, (w+size)*0.5, (h+size)*0.5)) 22 return xp.asarray(image, dtype=np.float32).transpose(2, 0, 1) 23 24def gram_matrix(y): 25 b, ch, h, w = y.data.shape 26 features = F.reshape(y, (b, ch, w*h)) 27 gram = F.batch_matmul(features, features, transb=True)/np.float32(ch*w*h) 28 return gram 29 30def total_variation(x): 31 xp = cuda.get_array_module(x.data) 32 b, ch, h, w = x.data.shape 33 wh = Variable(xp.asarray([[[[1], [-1]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[1], [-1]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[1], [-1]]]], dtype=np.float32), volatile=x.volatile) 34 ww = Variable(xp.asarray([[[[1, -1]], [[0, 0]], [[0, 0]]], [[[0, 0]], [[1, -1]], [[0, 0]]], [[[0, 0]], [[0, 0]], [[1, -1]]]], dtype=np.float32), volatile=x.volatile) 35 return F.sum(F.convolution_2d(x, W=wh) ** 2) + F.sum(F.convolution_2d(x, W=ww) ** 2) 36 37parser = argparse.ArgumentParser(description='Real-time style transfer') 38parser.add_argument('--gpu', '-g', default=-1, type=int, 39 help='GPU ID (negative value indicates CPU)') 40parser.add_argument('--dataset', '-d', default='dataset', type=str, 41 help='dataset directory path (according to the paper, use MSCOCO 80k images)') 42parser.add_argument('--style_image', '-s', type=str, required=True, 43 help='style image path') 44parser.add_argument('--batchsize', '-b', type=int, default=1, 45 help='batch size (default value is 1)') 46parser.add_argument('--initmodel', '-i', default=None, type=str, 47 help='initialize the model from given file') 48parser.add_argument('--resume', '-r', default=None, type=str, 49 help='resume the optimization from snapshot') 50parser.add_argument('--output', '-o', default=None, type=str, 51 help='output model file path without extension') 52parser.add_argument('--lambda_tv', default=1e-6, type=float, 53 help='weight of total variation regularization according to the paper to be set between 10e-4 and 10e-6.') 54parser.add_argument('--lambda_feat', default=1.0, type=float) 55parser.add_argument('--lambda_style', default=5.0, type=float) 56parser.add_argument('--epoch', '-e', default=2, type=int) 57parser.add_argument('--lr', '-l', default=1e-3, type=float) 58parser.add_argument('--checkpoint', '-c', default=0, type=int) 59parser.add_argument('--image_size', default=256, type=int) 60args = parser.parse_args() 61 62batchsize = args.batchsize 63 64image_size = args.image_size 65n_epoch = args.epoch 66lambda_tv = args.lambda_tv 67lambda_f = args.lambda_feat 68lambda_s = args.lambda_style 69style_prefix, _ = os.path.splitext(os.path.basename(args.style_image)) 70output = style_prefix if args.output == None else args.output 71fs = os.listdir(args.dataset) 72imagepaths = [] 73for fn in fs: 74 base, ext = os.path.splitext(fn) 75 if ext == '.jpg' or ext == '.png': 76 imagepath = os.path.join(args.dataset,fn) 77 imagepaths.append(imagepath) 78n_data = len(imagepaths) 79print('num traning images:', n_data) 80n_iter = n_data // batchsize 81print(n_iter, 'iterations,', n_epoch, 'epochs') 82 83model = FastStyleNet() 84vgg = VGG() 85serializers.load_npz('vgg16.model', vgg) 86if args.initmodel: 87 print('load model from', args.initmodel) 88 serializers.load_npz(args.initmodel, model) 89if args.gpu >= 0: 90 cuda.get_device(args.gpu).use() 91 model.to_gpu() 92 vgg.to_gpu() 93xp = np if args.gpu < 0 else cuda.cupy 94 95O = optimizers.Adam(alpha=args.lr) 96O.setup(model) 97if args.resume: 98 print('load optimizer state from', args.resume) 99 serializers.load_npz(args.resume, O) 100 101style = vgg.preprocess(np.asarray(Image.open(args.style_image).convert('RGB').resize((image_size,image_size)), dtype=np.float32)) 102style = xp.asarray(style, dtype=xp.float32) 103style_b = xp.zeros((batchsize,) + style.shape, dtype=xp.float32) 104for i in range(batchsize): 105 style_b[i] = style 106feature_s = vgg(Variable(style_b)) 107gram_s = [gram_matrix(y) for y in feature_s] 108 109for epoch in range(n_epoch): 110 print('epoch', epoch) 111 for i in range(n_iter): 112 model.zerograds() 113 vgg.zerograds() 114 115 indices = range(i * batchsize, (i+1) * batchsize) 116 x = xp.zeros((batchsize, 3, image_size, image_size), dtype=xp.float32) 117 for j in range(batchsize): 118 x[j] = load_image(imagepaths[i*batchsize + j], image_size) 119 120 xc = Variable(x.copy(), volatile=True) 121 x = Variable(x) 122 123 y = model(x) 124 125 xc -= 120 126 y -= 120 127 128 feature = vgg(xc) 129 feature_hat = vgg(y) 130 131 L_feat = lambda_f * F.mean_squared_error(Variable(feature[2].data), feature_hat[2]) # compute for only the output of layer conv3_3 132 133 L_style = Variable(xp.zeros((), dtype=np.float32)) 134 for f, f_hat, g_s in zip(feature, feature_hat, gram_s): 135 L_style += lambda_s * F.mean_squared_error(gram_matrix(f_hat), Variable(g_s.data)) 136 137 L_tv = lambda_tv * total_variation(y) 138 L = L_feat + L_style + L_tv 139 140 print('(epoch {}) batch {}/{}... training loss is...{}'.format(epoch, i, n_iter, L.data)) 141 142 L.backward() 143 O.update() 144 145 if args.checkpoint > 0 and i % args.checkpoint == 0: 146 serializers.save_npz('models/{}_{}_{}.model'.format(output, epoch, i), model) 147 serializers.save_npz('models/{}_{}_{}.state'.format(output, epoch, i), O) 148 149 print('save "style.model"') 150 serializers.save_npz('models/{}_{}.model'.format(output, epoch), model) 151 serializers.save_npz('models/{}_{}.state'.format(output, epoch), O) 152 153serializers.save_npz('models/{}.model'.format(output), model) 154serializers.save_npz('models/{}.state'.format(output), O)
Traceback (most recent call last):
File "train.py", line 118, in <module>
x[j] = load_image(imagepaths[i*batchsize + j],image_size)
File "train.py", line 22, in load_image
return xp.asarray(image, dtype=np.float32).transpose(2, 0, 1)
File "/home/kokawa2003/.local/lib/python2.7/site-packages/numpy/core/numeric.py", line 492, in asarray
return array(a, dtype, copy=False, order=order)
TypeError: float() argument must be a string or a number
ということなので
return xp.asarray(image, dtype=np.float32).transpose(2, 0, 1)
を
return xp.asarray(image, dtype=np.int32).transpose(2, 0, 1)
に変えてもエラーのままです
こんなエラーです
TypeError: long() argument must be a string or a number, not '_ImageCrop'
これどう考えても
image.cropの戻り値のキャストが変の意味だと思いますがどうするのが正解か分かりません。
pythonに詳しい人はお教えください
回答2件
あなたの回答
tips
プレビュー