前提・実現したいこと
技術評論社の「はじめての深層学習プログラミング」に沿って深層学習を勉強中です。
同本の80ページにあるサンプルプログラムをそのまま書き写しています。
発生している問題・エラーメッセージ
発生している問題は2つあります。
一つは書いている途中で下記コードの48行目で次のようなエラーが出ます。
undefined name 'cmp'
また、それを無視して実行すると、次のようなエラーが出ました。
Traceback (most recent call last): File "<ipython-input-1-4ff9c2ad1dbb>", line 1, in <module> runfile('C:/Python_Programs/Deep_Learning/de_le1.py', wdir='C:/Python_Programs/Deep_Learning') File "C:\Users\user\Anaconda3\envs\myTensorFlow\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile execfile(filename, namespace) File "C:\Users\user\Anaconda3\envs\myTensorFlow\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Python_Programs/Deep_Learning/de_le1.py", line 10, in <module> import cPickle as pickle ModuleNotFoundError: No module named 'cPickle'
該当のソースコード
python
1# -*- coding: utf-8 -*- 2 3import numpy as np 4import chainer 5import chainer.functions as cFunc 6import chainer.links as cLink 7import argparse 8import sys 9from PIL import Image 10import cPickle as pickle 11import math 12from chainer import serializers 13 14parser=argparse.ArgumentParser(description='Image inspection using chainer') 15parser.add_argument('image',help='Path to inspection image file') 16parser.add_argument('--model',default='model.h5',help='Path of the mean file (computed by compue_mean.py)') 17parser.add_argument('--mean',default='mean.npy',help='Path of the mean file (computed by compue_mean.py)') 18args=parser.parse_args() 19 20def read_image(path): 21 image=np.asarray(Image.open(path)).transpose(2,0,1) 22 top=left=cropwidth/2 23 bottom=model.insize+top 24 right=model.insize+left 25 image=image[:,top:bottom,left:right].astype(np.float32) 26 image-=mean_image[:,top:bottom,left:right] 27 image/=255 28 return image 29 30import network 31 32mean_image=pickle.load(open(args.mean,'rb')) 33 34model=network.Network() 35serializers.load_hdf5(args.model,model) 36cropwidth=256-model.insize 37model.to_cpu() 38model.train=False 39 40img=read_image(args.image) 41 42score=model.predict(np.array([img])) 43 44categories=np.loadtxt("labels.txt",str,delimiter="/t") 45 46top_k=20 47prediction=zip(score.data[0].tolist(),categories) 48prediction.sort(cmp=lambda x,y:cmp(x[0],y[0]),reverse=True) #ここでcmpのエラー発生 49 50for rank,(score,name) in enumerate(prediction[:top_k],start=1): 51 print('#%d | %s | %4.1f%%' % (rank,name,score*100)) 52
試したこと
この本はところどころ情報が古かったので、今回もそれの可能性があった。
で、調べてみたが特にそういった記述は見つからなかった。
入力ミスも考えたが、特に見つけられなかった。
補足情報(FW/ツールのバージョンなど)
使用した環境はWindows10、Anaconda3.6
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/14 15:26