お世話になります
最近 機械学習を 学び始めました
実行途中の重みデータですが
プログラム中のどこで 読み込めばよいかが 解りません
次のコードでは summary 表示後にしていますが
コメントのところの方が 適切でしょうか?
よろしく お願い致します
python
1import os 2 3import keras.backend as K 4K.set_image_data_format('channels_first') 5from keras.models import Sequential, Model 6from keras.layers import Conv2D, ZeroPadding2D, BatchNormalization, Input 7from keras.layers import Conv2DTranspose, Reshape, Activation, Cropping2D, Flatten 8from keras.layers.advanced_activations import LeakyReLU 9from keras.activations import relu 10from keras.initializers import RandomNormal 11conv_init = RandomNormal(0, 0.02) 12gamma_init = RandomNormal(1., 0.02) 13 14 15def DCGAN_D(isize, nz, nc, ndf, n_extra_layers=0): 16 assert isize%2==0 17 _ = inputs = Input(shape=(nc, isize, isize)) 18 _ = Conv2D(filters=ndf, kernel_size=4, strides=2, use_bias=False, 19 padding = "same", 20 kernel_initializer = conv_init, 21 name = 'initial.conv.{0}-{1}'.format(nc, ndf) 22 ) (_) 23 _ = LeakyReLU(alpha=0.2, name = 'initial.relu.{0}'.format(ndf))(_) 24 csize, cndf = isize// 2, ndf 25 while csize > 5: 26 assert csize%2==0 27 in_feat = cndf 28 out_feat = cndf*2 29 _ = Conv2D(filters=out_feat, kernel_size=4, strides=2, use_bias=False, 30 padding = "same", 31 kernel_initializer = conv_init, 32 name = 'pyramid.{0}-{1}.conv'.format(in_feat, out_feat) 33 ) (_) 34 if 0: # toggle batchnormalization 35 _ = BatchNormalization(name = 'pyramid.{0}.batchnorm'.format(out_feat), 36 momentum=0.9, axis=1, epsilon=1.01e-5, 37 gamma_initializer = gamma_init, 38 )(_, training=1) 39 _ = LeakyReLU(alpha=0.2, name = 'pyramid.{0}.relu'.format(out_feat))(_) 40 csize, cndf = (csize+1)//2, cndf*2 41 _ = Conv2D(filters=1, kernel_size=csize, strides=1, use_bias=False, 42 kernel_initializer = conv_init, 43 name = 'final.{0}-{1}.conv'.format(cndf, 1) 44 ) (_) 45 outputs = Flatten()(_) 46 return Model(inputs=inputs, outputs=outputs) 47 48 49 50 51def DCGAN_G(isize, nz, nc, ngf, n_extra_layers=0): 52 cngf= ngf//2 53 tisize = isize 54 while tisize > 5: 55 cngf = cngf * 2 56 assert tisize%2==0 57 tisize = tisize // 2 58 _ = inputs = Input(shape=(nz,)) 59 _ = Reshape((nz, 1,1))(_) 60 _ = Conv2DTranspose(filters=cngf, kernel_size=tisize, strides=1, use_bias=False, 61 kernel_initializer = conv_init, 62 name = 'initial.{0}-{1}.convt'.format(nz, cngf))(_) 63 _ = BatchNormalization(gamma_initializer = gamma_init, momentum=0.9, axis=1, epsilon=1.01e-5, 64 name = 'initial.{0}.batchnorm'.format(cngf))(_, training=1) 65 _ = Activation("relu", name = 'initial.{0}.relu'.format(cngf))(_) 66 csize, cndf = tisize, cngf 67 68 69 while csize < isize//2: 70 in_feat = cngf 71 out_feat = cngf//2 72 _ = Conv2DTranspose(filters=out_feat, kernel_size=4, strides=2, use_bias=False, 73 kernel_initializer = conv_init, padding="same", 74 name = 'pyramid.{0}-{1}.convt'.format(in_feat, out_feat) 75 ) (_) 76 _ = BatchNormalization(gamma_initializer = gamma_init, 77 momentum=0.9, axis=1, epsilon=1.01e-5, 78 name = 'pyramid.{0}.batchnorm'.format(out_feat))(_, training=1) 79 80 _ = Activation("relu", name = 'pyramid.{0}.relu'.format(out_feat))(_) 81 csize, cngf = csize*2, cngf//2 82 _ = Conv2DTranspose(filters=nc, kernel_size=4, strides=2, use_bias=False, 83 kernel_initializer = conv_init, padding="same", 84 name = 'final.{0}-{1}.convt'.format(cngf, nc) 85 )(_) 86 outputs = Activation("tanh", name = 'final.{0}.tanh'.format(nc))(_) 87 return Model(inputs=inputs, outputs=outputs) 88 89 90 91 92nc = 3 93nz = 100 94ngf = 8 95ndf = 8 96n_extra_layers = 0 97Diters = 5 98λ = 10 99 100imageSize = 256 101batchSize = 16 102lrD = 1e-4 103lrG = 1e-4 104 105 106 107netD = DCGAN_D(imageSize, nz, nc, ndf, n_extra_layers) 108netD.summary() 109 110 111netG = DCGAN_G(imageSize, nz, nc, ngf, n_extra_layers) 112netG.summary() 113 114 115try: 116 netG.load_weights('./netG.hdf5') 117 netD.load_weights('./netD.hdf5') 118 print('重みの読み込み完了') 119except: 120 pass 121 122 123 124from keras.optimizers import RMSprop, SGD, Adam 125 126 127 128netD_real_input = Input(shape=(nc, imageSize, imageSize)) 129noisev = Input(shape=(nz,)) 130netD_fake_input = netG(noisev) 131 132ϵ_input = K.placeholder(shape=(None,1,1,1)) 133netD_mixed_input = Input(shape=(nc, imageSize, imageSize), 134 tensor=ϵ_input * netD_real_input + (1-ϵ_input) * netD_fake_input) 135 136 137loss_real = K.mean(netD(netD_real_input)) 138loss_fake = K.mean(netD(netD_fake_input)) 139 140grad_mixed = K.gradients(netD(netD_mixed_input), [netD_mixed_input])[0] 141norm_grad_mixed = K.sqrt(K.sum(K.square(grad_mixed), axis=[1,2,3])) 142grad_penalty = K.mean(K.square(norm_grad_mixed -1)) 143 144loss = loss_fake - loss_real + λ * grad_penalty 145 146 147training_updates = Adam(lr=lrD, beta_1=0.0, beta_2=0.9).get_updates(netD.trainable_weights,[],loss) 148netD_train = K.function([netD_real_input, noisev, ϵ_input], 149 [loss_real, loss_fake], 150 training_updates) 151 152 153 154 155loss = -loss_fake 156training_updates = Adam(lr=lrG, beta_1=0.0, beta_2=0.9).get_updates(netG.trainable_weights,[], loss) 157netG_train = K.function([noisev], [loss], training_updates) 158 159""" 160************************************************* 161# ここで load した方が 適切か? 162************************************************* 163""" 164 165 166 167from PIL import Image 168import numpy as np 169import tarfile 170 171 172from tensorflow.python.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array, array_to_img 173import glob 174import cv2 175from keras.preprocessing import image 176import os 177 178train_X=[] 179train_y=[] 180files = glob.glob('/content/drive/train/*.*') 181 182for img_file in files: 183 img = cv2.imread(img_file) 184 img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 185 img = cv2.resize(img,(256,256),interpolation = cv2.INTER_AREA) 186 img = np.transpose(img,(2,0,1)) 187 188 train_X.extend(img.reshape(-1,3,256,256)/255*2-1) 189 190 191 192train_X = np.array(train_X) 193 194train_X = np.concatenate([train_X[:,:,:,::-1], train_X]) 195 196print('画像データの読み込みが終了しました') 197 198 199from IPython.display import display 200def showX(X, rows=1): 201 assert X.shape[0]%rows == 0 202 int_X = ( (X+1)/2*255).clip(0,255).astype('uint8') 203 204 int_X = np.moveaxis(int_X.reshape(-1,3,256,256), 1, 3) 205 int_X = int_X.reshape(rows, -1, 256, 256,3).swapaxes(1,2).reshape(rows*256,-1, 3) 206 207 (Image.fromarray(int_X)).save('./WGAN-GP_result.png') 208 209 210 211fixed_noise = np.random.normal(size=(batchSize, nz)).astype('float32') 212 213 214import time 215t0 = time.time() 216niter = 1000000 217gen_iterations = 0 218errG = 0 219targetD = np.float32([2]*batchSize+[-2]*batchSize)[:, None] 220targetG = np.ones(batchSize, dtype=np.float32)[:, None] 221for epoch in range(niter): 222 i = 0 223 224 np.random.shuffle(train_X) 225 batches = train_X.shape[0]//batchSize 226 while i < batches: 227 if gen_iterations < 25 or gen_iterations % 500 == 0: 228 _Diters = 100 229 else: 230 _Diters = Diters 231 j = 0 232 while j < _Diters and i < batches: 233 j+=1 234 real_data = train_X[i*batchSize:(i+1)*batchSize] 235 i+=1 236 noise = np.random.normal(size=(batchSize, nz)) 237 ϵ = np.random.uniform(size=(batchSize, 1, 1 ,1)) 238 errD_real, errD_fake = netD_train([real_data, noise, ϵ]) 239 errD = errD_real - errD_fake 240 241 if gen_iterations%500==0: 242 print('[%d/%d][%d/%d][%d] Loss_D: %f Loss_G: %f Loss_D_real: %f Loss_D_fake %f' 243 % (epoch, niter, i, batches, gen_iterations,errD, errG, errD_real, errD_fake), time.time()-t0) 244 fake = netG.predict(fixed_noise) 245 showX(fake, 2) 246 if gen_iterations % 5000 == 0: 247 netG.save_weights('./netG.hdf5') 248 netD.save_weights('./netD.hdf5') 249 250 noise = np.random.normal(size=(batchSize, nz)) 251 errG, = netG_train([noise]) 252 gen_iterations+=1
`
追記:
質問は
一度保存した重みデータを 再度実行するときに 読み込むという意味です
追記2:
質問のコードは こちらを元に
WGAN-GPの実行いたしたく手直ししたものです
Colab 上での起動のため (時間制限があるので) 重みデータを (Google Driveに) 一旦 保存して
次の実行の際 そのデータを 読み込むことを想定しております
機械学習初心者で はじめから構築は難しく
既存のプログラムの実行して 勉強しております
model 定義後か Optimizer 定義後か
どちらで 重みデータを 読み込むのが
適切なのかが 勉強不足のため わかりませんので
質問いたしました