def main(args): print('create images...') x, y = create(args.other_path, args.human_path, args.background_path, args.obj_size, args.img_size, args.obj_num, args.human_num, args.img_num) # 画像の並び順をシャッフルするための配列を作成する # compとrawの対応を崩さないようにシャッフルしなければならない # また、train_sizeで端数を切り捨てる # データをint8からfloat16に変えるとデータ数が大きくなるので注意 print('shuffle images...') dtype = np.float16 shuffle = np.random.permutation(range(len(x))) train_size = int(len(x) * args.train_per_all) train_x = IMG.imgs2arr(x[shuffle[:train_size]], dtype=dtype) train_y = IMG.imgs2arr(y[shuffle[:train_size]], dtype=dtype) test_x = IMG.imgs2arr(x[shuffle[train_size:]], dtype=dtype) test_y = IMG.imgs2arr(y[shuffle[train_size:]], dtype=dtype) print('train x/y:{0}/{1}'.format(train_x.shape, train_y.shape)) print('test x/y:{0}/{1}'.format(test_x.shape, test_y.shape)) print('save param...') F.dict2json(args.out_path, 'dataset', F.args2dict(args)) # 生成したデータをnpz形式でデータセットとして保存する # ここで作成したデータの中身を確認する場合はnpz2jpg.pyを使用するとよい print('save npz...') saveNPZ(train_x, train_y, 'train', args.out_path, args.img_size) saveNPZ(test_x, test_y, 'test', args.out_path, args.img_size)
def test_imgs2arr(self): l = cv2.imread(lenna_path) m = cv2.imread(mandrill_path) self.assertEqual(IMG.imgs2arr([l, m]).shape, (2, 3, 256, 256)) l = cv2.imread(lenna_path, IMG.getCh(1)) m = cv2.imread(mandrill_path, IMG.getCh(1)) self.assertEqual(IMG.imgs2arr([l, m]).shape, (2, 1, 256, 256))
def main(args): # 入力のカラー画像を読み込む if IMG.isImgPath(args.color): print('color image read:\t', args.color) x = cv2.imread(args.color, IMG.getCh(3)) else: print('[ERROR] color image not found:', args.color) exit() # 正解のモノクロ画像を読み込む if IMG.isImgPath(args.duotone): print('duotone image read:\t', args.duotone) y = edgeDetect(cv2.imread(args.duotone, IMG.getCh(1)), x) else: print('[ERROR] duotone image not found:', args.duotone) exit() # 入力画像と正解画像を # [1] 反転して増やす [flip()] # [2] 正方形に分割する [splitSQN()] print('split and rotate images...') x, _ = IMG.splitSQN(IMG.flip(x, args.augmentation), args.img_size, args.round) y, _ = IMG.splitSQN(IMG.flip(y, args.augmentation), args.img_size, args.round) # 画像の並び順をシャッフルするための配列を作成する # colorとmonoの対応を崩さないようにシャッフルしなければならない # また、train_sizeで端数を切り捨てる print('shuffle images...') shuffle = np.random.permutation(range(len(x))) train_size = int(len(x) * args.train_per_all) print('train/all : {0}/{1}'.format(train_size, len(x))) # float16で保存することでファイルサイズを軽減できる # 情報量は落ちるが問題ないレベル dtype = np.float16 train_x = IMG.imgs2arr(x[shuffle[:train_size]], dtype=dtype) train_y = IMG.imgs2arr(y[shuffle[:train_size]], dtype=dtype) test_x = IMG.imgs2arr(x[shuffle[train_size:]], dtype=dtype) test_y = IMG.imgs2arr(y[shuffle[train_size:]], dtype=dtype) print('train x/y:{0}/{1}'.format(train_x.shape, train_y.shape)) print('test x/y:{0}/{1}'.format(test_x.shape, test_y.shape)) # 生成したデータをnpz形式でデータセットとして保存する # ここで作成したデータの中身を確認する場合はnpz2jpg.pyを使用するとよい print('save npz...') saveNPZ(train_x, train_y, 'train', args.out_path, args.img_size) saveNPZ(test_x, test_y, 'test', args.out_path, args.img_size)
def predict(model, data, batch, org_shape, rate, gpu): """ 推論実行メイン部 [in] model: 推論実行に使用するモデル [in] data: 分割(IMG.split)されたもの [in] batch: バッチサイズ [in] org_shape: 分割前のshape [in] rate: 出力画像の拡大率 [in] gpu: GPU ID [out] img: 推論実行で得られた生成画像 """ # dataには圧縮画像と分割情報が含まれているので、分離する comp, size = data imgs = [] st = time.time() # バッチサイズごとに学習済みモデルに入力して画像を生成する for i in range(0, len(comp), batch): x = IMG.imgs2arr(comp[i:i + batch], gpu=gpu) y = model.predictor(x) imgs.extend(IMG.arr2imgs(to_cpu(y.array))) print('exec time: {0:.2f}[s]'.format(time.time() - st)) # 生成された画像を分割情報をもとに結合する buf = [ np.vstack(imgs[i * size[0]:(i + 1) * size[0]]) for i in range(size[1]) ] img = np.hstack(buf) # 出力画像は入力画像の2倍の大きさになっているので半分に縮小する img = IMG.resize(img, 1 / rate) # 結合しただけでは画像サイズがオリジナルと異なるので切り取る return img[:org_shape[0], :org_shape[1]]
def main(args): # OpenCV形式で画像を読み込むために # チャンネル数をOpenCVのフラグ形式に変換する ch = IMG.getCh(args.channel) # OpenCV形式で画像をリストで読み込む print('read images...') imgs = [cv2.imread(name, ch) for name in args.jpeg if IMG.isImgPath(name)] if len(imgs) == 0: print('image get error') exit(1) # 画像を圧縮して分割する(学習の入力データに相当) print('split images...') x, _ = IMG.splitSQN( IMG.flipN(IMG.encodeDecodeN(imgs, ch, args.quality), args.flip_num), args.img_size, args.round) # 画像を分割する(正解データに相当) y, _ = IMG.splitSQN(IMG.flipN(imgs, args.flip_num), args.img_size, args.round) # 画像の並び順をシャッフルするための配列を作成する # compとrawの対応を崩さないようにシャッフルしなければならない # また、train_sizeで端数を切り捨てる # データをint8からfloat16に変えるとデータ数が大きくなるので注意 print('shuffle images...') dtype = np.float16 shuffle = np.random.permutation(range(len(x))) train_size = int(len(x) * args.train_per_all) train_x = IMG.imgs2arr(x[shuffle[:train_size]], dtype=dtype) train_y = IMG.imgs2arr(y[shuffle[:train_size]], dtype=dtype) test_x = IMG.imgs2arr(x[shuffle[train_size:]], dtype=dtype) test_y = IMG.imgs2arr(y[shuffle[train_size:]], dtype=dtype) print('train x/y:{0}/{1}'.format(train_x.shape, train_y.shape)) print('test x/y:{0}/{1}'.format(test_x.shape, test_y.shape)) # 生成したデータをnpz形式でデータセットとして保存する # ここで作成したデータの中身を確認する場合はnpz2jpg.pyを使用するとよい print('save npz...') saveNPZ(train_x, train_y, 'train', args.out_path, args.img_size) saveNPZ(test_x, test_y, 'test', args.out_path, args.img_size)
def predict(model, img, batch, gpu): """ 推論実行メイン部 [in] model: 推論実行に使用するモデル [in] img: 入力画像 [in] batch: バッチサイズ [in] gpu: GPU ID [out] img: 推論実行で得られた生成画像 """ # dataには圧縮画像と分割情報が含まれているので、分離する st = time.time() # バッチサイズごとに学習済みモデルに入力して画像を生成する x = IMG.imgs2arr(img, gpu=gpu) y = model.predictor(x) predict_img = IMG.arr2imgs(to_cpu(y.array))[0] print('exec time: {0:.2f}[s]'.format(time.time() - st)) return predict_img