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

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

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

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

Q&A

0回答

2626閲覧

DCGANを動かそうとしたのですがCURANDErrorが…

RMs

総合スコア6

Python 2.7

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

0グッド

0クリップ

投稿2017/01/27 12:18

###前提・実現したいこと
ここに質問したいことを詳細に書いてください
Anacondan2.7のSpyderでGPU環境で動かそうとしたときに発生しました
DCGANをやろうとGPUを何とか動かそうとしたときに発生しました
OS Windows10
CUDA 8.0
GPU GTX 1050
###発生している問題・エラーメッセージ

エラーメッセージ

File "cupy/cuda/curand.pyx", line 148, in cupy.cuda.curand.generateUniform (cupy\cuda\curand.cpp:2634)

File "cupy/cuda/curand.pyx", line 151, in cupy.cuda.curand.generateUniform (cupy\cuda\curand.cpp:2536)

File "cupy/cuda/curand.pyx", line 85, in cupy.cuda.curand.check_status (cupy\cuda\curand.cpp:1224)

CURANDError: CURAND_STATUS_LAUNCH_FAILURE

###該当のソースコード
python
ここにご自身が実行したソースコードを書いてください
import pickle
import numpy as np
from PIL import Image
import os
from StringIO import StringIO
import math
import pylab

import chainer
from chainer import computational_graph
from chainer import cuda
from chainer import optimizers
from chainer import serializers
from chainer import Variable
from chainer.utils import type_check
from chainer import function

import chainer.functions as F
import chainer.links as L

import numpy

image_dir = './images'
out_image_dir = './out_images'
out_model_dir = './out_models'

nz = 100 # # of dim for Z
batchsize=100
n_epoch=10000
n_train=200000
image_save_interval = 50000

read all images

fs = os.listdir(image_dir)
print len(fs)
dataset = []
for fn in fs:
f = open('%s/%s'%(image_dir,fn), 'rb')
img_bin = f.read()
dataset.append(img_bin)
f.close()
print len(dataset)

class ELU(function.Function):

"""Exponential Linear Unit.""" # https://github.com/muupan/chainer-elu def __init__(self, alpha=1.0): self.alpha = numpy.float32(alpha) def check_type_forward(self, in_types): type_check.expect(in_types.size() == 1) x_type, = in_types type_check.expect( x_type.dtype == numpy.float32, ) def forward_cpu(self, x): y = x[0].copy() neg_indices = x[0] < 0 y[neg_indices] = self.alpha * (numpy.exp(y[neg_indices]) - 1) return y, def forward_gpu(self, x): y = cuda.elementwise( 'T x, T alpha', 'T y', 'y = x >= 0 ? x : alpha * (exp(x) - 1)', 'elu_fwd')( x[0], self.alpha) return y, def backward_cpu(self, x, gy): gx = gy[0].copy() neg_indices = x[0] < 0 gx[neg_indices] *= self.alpha * numpy.exp(x[0][neg_indices]) return gx, def backward_gpu(self, x, gy): gx = cuda.elementwise( 'T x, T gy, T alpha', 'T gx', 'gx = x >= 0 ? gy : gy * alpha * exp(x)', 'elu_bwd')( x[0], gy[0], self.alpha) return gx,

def elu(x, alpha=1.0):
"""Exponential Linear Unit function."""
# https://github.com/muupan/chainer-elu
return ELU(alpha=alpha)(x)

class Generator(chainer.Chain):
def init(self):
super(Generator, self).init(
l0z = L.Linear(nz, 66512, wscale=0.02math.sqrt(nz)),
dc1 = L.Deconvolution2D(512, 256, 4, stride=2, pad=1, wscale=0.02
math.sqrt(44512)),
dc2 = L.Deconvolution2D(256, 128, 4, stride=2, pad=1, wscale=0.02math.sqrt(44256)),
dc3 = L.Deconvolution2D(128, 64, 4, stride=2, pad=1, wscale=0.02
math.sqrt(44128)),
dc4 = L.Deconvolution2D(64, 3, 4, stride=2, pad=1, wscale=0.02math.sqrt(4464)),
bn0l = L.BatchNormalization(6
6*512),
bn0 = L.BatchNormalization(512),
bn1 = L.BatchNormalization(256),
bn2 = L.BatchNormalization(128),
bn3 = L.BatchNormalization(64),
)

def __call__(self, z, test=False): h = F.reshape(F.relu(self.bn0l(self.l0z(z), test=test)), (z.data.shape[0], 512, 6, 6)) h = F.relu(self.bn1(self.dc1(h), test=test)) h = F.relu(self.bn2(self.dc2(h), test=test)) h = F.relu(self.bn3(self.dc3(h), test=test)) x = (self.dc4(h)) return x

class Discriminator(chainer.Chain):
def init(self):
super(Discriminator, self).init(
c0 = L.Convolution2D(3, 64, 4, stride=2, pad=1, wscale=0.02math.sqrt(443)),
c1 = L.Convolution2D(64, 128, 4, stride=2, pad=1, wscale=0.02
math.sqrt(4464)),
c2 = L.Convolution2D(128, 256, 4, stride=2, pad=1, wscale=0.02math.sqrt(44128)),
c3 = L.Convolution2D(256, 512, 4, stride=2, pad=1, wscale=0.02
math.sqrt(44256)),
l4l = L.Linear(66512, 2, wscale=0.02math.sqrt(66*512)),
bn0 = L.BatchNormalization(64),
bn1 = L.BatchNormalization(128),
bn2 = L.BatchNormalization(256),
bn3 = L.BatchNormalization(512),
)

def __call__(self, x, test=False): h = elu(self.c0(x)) # no bn because images from generator will katayotteru? h = elu(self.bn1(self.c1(h), test=test)) h = elu(self.bn2(self.c2(h), test=test)) h = elu(self.bn3(self.c3(h), test=test)) l = self.l4l(h) return l

def clip_img(x):
return np.float32(-1 if x<-1 else (1 if x>1 else x))

def train_dcgan_labeled(gen, dis, epoch0=0):
o_gen = optimizers.Adam(alpha=0.0002, beta1=0.5)
o_dis = optimizers.Adam(alpha=0.0002, beta1=0.5)
o_gen.setup(gen)
o_dis.setup(dis)
o_gen.add_hook(chainer.optimizer.WeightDecay(0.00001))
o_dis.add_hook(chainer.optimizer.WeightDecay(0.00001))

zvis = (xp.random.uniform(-1, 1, (100, nz), dtype=np.float32)) for epoch in xrange(epoch0,n_epoch): perm = np.random.permutation(n_train) sum_l_dis = np.float32(0) sum_l_gen = np.float32(0) for i in xrange(0, n_train, batchsize): # discriminator # 0: from dataset # 1: from noise #print "load image start ", i x2 = np.zeros((batchsize, 3, 96, 96), dtype=np.float32) for j in range(batchsize): try: rnd = np.random.randint(len(dataset)) rnd2 = np.random.randint(2) img = np.asarray(Image.open(StringIO(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 except: print 'read image error occured', fs[rnd] #print "load image done" # train generator z = Variable(xp.random.uniform(-1, 1, (batchsize, nz), dtype=np.float32)) x = gen(z) yl = dis(x) L_gen = F.softmax_cross_entropy(yl, Variable(xp.zeros(batchsize, dtype=np.int32))) L_dis = F.softmax_cross_entropy(yl, Variable(xp.ones(batchsize, dtype=np.int32))) # train discriminator x2 = Variable(cuda.to_gpu(x2)) yl2 = dis(x2) L_dis += F.softmax_cross_entropy(yl2, Variable(xp.zeros(batchsize, dtype=np.int32))) #print "forward done" o_gen.zero_grads() L_gen.backward() o_gen.update() o_dis.zero_grads() L_dis.backward() o_dis.update() sum_l_gen += L_gen.data.get() sum_l_dis += L_dis.data.get() #print "backward done" if i%image_save_interval==0: pylab.rcParams['figure.figsize'] = (16.0,16.0) pylab.clf() vissize = 100 z = zvis z[50:,:] = (xp.random.uniform(-1, 1, (50, nz), dtype=np.float32)) z = Variable(z) x = gen(z, test=True) x = x.data.get() for i_ in range(100): tmp = ((np.vectorize(clip_img)(x[i_,:,:,:])+1)/2).transpose(1,2,0) pylab.subplot(10,10,i_+1) pylab.imshow(tmp) pylab.axis('off') pylab.savefig('%s/vis_%d_%d.png'%(out_image_dir, epoch,i)) serializers.save_hdf5("%s/dcgan_model_dis_%d.h5"%(out_model_dir, epoch),dis) serializers.save_hdf5("%s/dcgan_model_gen_%d.h5"%(out_model_dir, epoch),gen) serializers.save_hdf5("%s/dcgan_state_dis_%d.h5"%(out_model_dir, epoch),o_dis) serializers.save_hdf5("%s/dcgan_state_gen_%d.h5"%(out_model_dir, epoch),o_gen) print 'epoch end', epoch, sum_l_gen/n_train, sum_l_dis/n_train

xp = cuda.cupy
cuda.get_device(0).use()

gen = Generator()
dis = Discriminator()
gen.to_gpu()
dis.to_gpu()

try:
os.mkdir(out_image_dir)
os.mkdir(out_model_dir)
except:
pass

train_dcgan_labeled(gen, dis)

###試したこと
課題に対してアプローチしたことを記載してください

###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問