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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

Q&A

解決済

2回答

2770閲覧

Pythonで型エラー

kokawa2003

総合スコア217

Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

0グッド

0クリップ

投稿2018/05/24 02:14

編集2018/05/24 04:37

突然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に詳しい人はお教えください

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

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

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

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

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

can110

2018/05/24 06:06

「return xp.asarray(image, dtype=np.float32).transpose(2, 0, 1)」の「xp」には何が入っていますか?(np?cuda.cupy?)
wakame

2018/05/24 08:58

コードはchainerなんですか取り組んでいるのはtensorflowなんですか?
wakame

2018/05/24 09:03

上記のリンクではchainer ver 1.7.1で動作実績があるようです。あなたが使われているchainerのversionを教えてください。
kokawa2003

2018/05/24 10:14

chainerの最新をpipで入れたので4.0です。環境はubuntu16.04です。r 1.7.1はpipで入れようとしたけど4.0をuninstして入れたがエラーでできませんでした
kokawa2003

2018/05/24 10:30

またソースをフルで記述しました。このソースはhttps://github.com/yusuketomoto/chainer-fast-neuralstyleから入手して実行エラーを直しているものです
kokawa2003

2018/05/24 10:35

またPCにはchainerとtensorflowどちらもあります。また上の1.7.1 を入れてみたというのは間違いでsudo pip install chainer==1.17.0 がOKらしいので入れて見たけどだめでした。
kokawa2003

2018/05/24 10:58

あと1.7.1を入れてみました。sudo pip uninstall chainerののちsudo pip install chainer==1.7.1をしましたがinstallの最後でCommand "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-euODb0/chainer/のエラーになります
kokawa2003

2018/05/24 11:00

あとバージョンは正確にはSuccessfully installed chainer-4.1.0です
guest

回答2

0

自己解決

https://github.com/yusuketomoto/chainer-fast-neuralstyle/issues/40
が正解
In conclusion, modify train.py line 23
image = image.crop((int((w-size)*0.5), int((h-size)*0.5), int((w+size)*0.5), int((h+size)*0.5)))

投稿2018/05/24 14:20

kokawa2003

総合スコア217

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

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

kokawa2003

2018/05/24 14:40

とりあえず経緯を軽くまとめます。 githubで検索するとhttps://github.com/huwenyue/chainer-fast-neuralstyle が5日前なのでこれを落とす。 trainすると失敗するので https://github.com/yusuketomoto/chainer-fast-neuralstyle/issues/40 をした。 またtrainのコマンドは私の環境で python train.py -s /home/kokawa2003/Downloads/anime/anime.png -d /home/kokawa2003/Downloads/train2014 でOK chainer-4.1.0でOK 多分最新でOKだと思う。
guest

0

型指定を外して

python

1xp.asarray(image) 2np.asarray(image)

してみてください。特に問題なければ入力データの型になるはずなんですが・・・。

投稿2018/05/24 02:37

hayataka2049

総合スコア30933

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

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

kokawa2003

2018/05/24 03:07

return np.asarray(image).transpose(2, 0, 1)に変更すると新しいエラーで ValueError: axes don't match array でした。  return xp.asarray(image).transpose(2, 0, 1)でも同じでした。
hayataka2049

2018/05/24 09:02 編集

transposeで出ているエラーなので、とりあえず配列には変換できているようです。 xp.asarray(image)の返り値のshapeを確認してみてください。 デバッグのために、一回変数に格納しておいた方が良いでしょう。コードの変更例を示します。 image_array = xp.asarray(image) # print image_array.shape等を挿入。ついでにdtypeも確認で見てみましょう transposed_image_array = image_array.transpose(2, 0, 1) return transposed_image_array
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問