help='The filename of mask, value 255 indicates mask.') parser.add_argument('--output', default='output.png', type=str, help='Where to write output.') parser.add_argument('--checkpoint_dir', default='', type=str, help='The directory of tensorflow checkpoint.') if __name__ == "__main__": print("In main") ng.get_gpus(1) args = parser.parse_args() model = InpaintCAModel() image = cv2.imread(args.image) mask = cv2.imread(args.mask) assert image.shape == mask.shape h, w, _ = image.shape grid = 8 image = image[:h // grid * grid, :w // grid * grid, :] mask = mask[:h // grid * grid, :w // grid * grid, :] print('Shape of image: {}'.format(image.shape)) image = np.expand_dims(image, 0) mask = np.expand_dims(mask, 0) input_image = np.concatenate([image, mask], axis=2)
if __name__ == "__main__": #TODO: 실행 전에 터지지 않는 적절한 값으로 딱 한번 초기화 할 것. core.seg_limit = 4000000 #// 10 # 보통 이게 더 큼 core.compl_limit = 1000000 #// 10 # segnet_yml = 'segnet/seg48_4[553].yml' # segnet configuration segnet_model_path = 'segnet/seg48_4[553].h5' # saved segnet model complnet_ckpt_dir = 'v2_180923' # saved complnets directory #-------------------------------------------- # for segnet with open(segnet_yml, 'r') as f: config = yaml.load(f) # for complnet complnet = InpaintCAModel('v2') ng.get_gpus(1, False) #TODO: 단 한 번만 호출할 것. #-------------------------------------------- dilate_kernel = core.rect5 if not os.path.isdir('./cleaned'): os.mkdir('./cleaned') app = QtWidgets.QApplication(sys.argv) screen = app.primaryScreen() print('Screen: %s' % screen.name()) size = screen.size() print('Size: %d x %d' % (size.width(), size.height())) rect = screen.availableGeometry() print('Available: %d x %d' % (rect.width(), rect.height())) MainWindow = MyMainScreen()
def iter_generate_image(image, bbox, id, checkpoint_dir='model_logs/release_imagenet_256', iterate=False): # ng.get_gpus(1) model = InpaintCAModel() mask_obj = Mask_obj(top=bbox['y'], left=bbox['x'], width=bbox['w'], height=bbox['h']) mask = mask_obj.image assert image.shape == mask.shape print('Shape of image: {}'.format(image.shape)) iter1_input = preprocess_image(image, mask) input_shape = iter1_input.shape sess_config = tf.ConfigProto() sess_config.gpu_options.allow_growth = True with tf.Session(config=sess_config) as sess: # input_image = tf.constant(input_image, dtype=tf.float32) input_image = tf.placeholder(tf.float32, shape=input_shape) output = model.build_server_graph(input_image) output = (output + 1.) * 127.5 output = tf.reverse(output, [-1]) output = tf.saturate_cast(output, tf.uint8) # load pretrained model vars_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) assign_ops = [] for var in vars_list: vname = var.name from_name = vname var_value = tf.contrib.framework.load_variable( checkpoint_dir, from_name) assign_ops.append(tf.assign(var, var_value)) sess.run(assign_ops) print('Model loaded.') output_images = [] # iter1 result = sess.run(output, feed_dict={input_image: iter1_input}) iter1_image = result[0][:, :, ::-1] output_images.append(iter1_image) if iterate: # iter2 iter2_submask_objs = generate_submask_objs([mask_obj]) temp_image = iter1_image for s in iter2_submask_objs: submask = s.image temp_input = preprocess_image(temp_image, submask) result = sess.run(output, feed_dict={input_image: temp_input}) temp_image = result[0][:, :, ::-1] iter2_image = temp_image output_images.append(iter2_image) # iter3 # iter3_submask_objs = generate_submask_objs(iter2_submask_objs) # temp_image = iter2_image # for s in iter3_submask_objs: # submask = s.image # temp_input = preprocess_image(temp_image, submask) # result = sess.run(output, feed_dict={input_image: temp_input}) # temp_image = result[0][:, :, ::-1] # iter3_image = temp_image # output_images.append(iter3_image) print('hi') return output_images[-1]
def complete(image_file): print("In complete") #ng.get_gpus(1,verbose=False) tf.reset_default_graph() model = InpaintCAModel() image = cv2.imread(os.path.join(args.image_dir, image_file)) mask = cv2.imread(os.path.join(args.mask_dir, image_file)) assert image.shape == mask.shape h, w, _ = image.shape image_small = image mask_small = mask make_small_flag = False if h > 1700 or w > 1700: print("girdimm") image_small = image_small[:, 90:-90, :] mask_small = mask_small[:, 90:-90, :] h, w, _ = image_small.shape make_small_flag = True print("image shape:", image.shape) print("image small shape:", image_small.shape) grid = 8 image_rs = image_small[:h // grid * grid, :w // grid * grid, :] mask_rs = mask_small[:h // grid * grid, :w // grid * grid, :] print('image rs shape: {}'.format(image_rs.shape)) image_rs = np.expand_dims(image_rs, 0) mask_rs = np.expand_dims(mask_rs, 0) input_image = np.concatenate([image_rs, mask_rs], axis=2) sess_config = tf.ConfigProto() sess_config.gpu_options.allow_growth = True with tf.Session(config=sess_config) as sess: input_image = tf.constant(input_image, dtype=tf.float32) output = model.build_server_graph(input_image) output = (output + 1.) * 127.5 output = tf.reverse(output, [-1]) output = tf.saturate_cast(output, tf.uint8) # load pretrained model vars_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) assign_ops = [] for var in vars_list: vname = var.name from_name = vname var_value = tf.contrib.framework.load_variable( args.checkpoint_dir, from_name) assign_ops.append(tf.assign(var, var_value)) sess.run(assign_ops) result = sess.run(output) image_small[:h // grid * grid, :w // grid * grid, :] = result[0][:, :, ::-1] if make_small_flag == True: image[:, 90:-90, :] = image_small save_value = cv2.imwrite(os.path.join(args.output_dir, image_file), image) print("Image small saved:", save_value) else: save_value = cv2.imwrite(os.path.join(args.output_dir, image_file), image_small) print("Image saved:", save_value) sess.close()