def do_processing(path_to_file): # Choose which CPU or GPU card to use dev = mx.cpu() ctx = dev # Params long_edge = args.long_edge # Load data dir_sketch = "data/sk/" file_sketch = os.listdir(dir_sketch) file_sketch.sort() training = int(0.7 * len(file_sketch)) # 70% num_sketch = int(len(file_sketch) - training) # 30% logging.info("Num of sketches: %d" % num_sketch) print(dir_sketch + file_sketch[training]) # Init sketch sketch_np = data_processing.PreprocessSketchImage(path_to_file, long_edge) logging.info("load the sketch image, size = %s", sketch_np.shape[2:]) dshape = sketch_np.shape clip_norm = 0.05 * np.prod(dshape) # Load pretrained params gens = dcnn.get_module("g0", dshape, ctx) gens.load_params("model/0265-0011975-sketch-vgg16.params") # Testing logging.info('Start testing arguments %s', args) # Load sketch sketch_data = [] path_sketch = dir_sketch + file_sketch[training] sketch_np = data_processing.PreprocessSketchImage(path_to_file, long_edge) mySketch = mx.nd.array(sketch_np) sketch_data.append(mySketch) gens.forward(mx.io.DataBatch([sketch_data[-1]], [0]), is_train=False) new_img = gens.get_outputs()[0] #cv2.namedWindow('output', cv2.WINDOW_NORMAL) #new_image=new_img.asnumpy() #print(new_image) print(file_sketch[training]) #cv2.destroyAllWindows() data_processing.SaveImage(new_img.asnumpy(), "output/test/out_%s" % file_sketch[training]) new_image = cv2.imread("output/test/out_%s" % file_sketch[training]) cv2.imshow('img', new_image) cv2.waitKey(10)
def do_processing(path_to_file): # Choose which CPU or GPU card to use dev = mx.gpu(args.gpu) if args.gpu >= 0 else mx.cpu() ctx = dev # Params long_edge = args.long_edge # load data img = cv2.imread(path_to_file) img_path = "/home/jo/mainproject/sketch/data/sk/images1.jpg" # Init sketch sketch_np = data_processing.PreprocessSketchImage(img_path, long_edge) print("Colored") # sketch_np = data_processing.PreprocessSketchImage(dir_sketch + file_sketch[training], long_edge) logging.info("load the sketch image, size = %s", sketch_np.shape[2:]) dshape = sketch_np.shape clip_norm = 0.05 * np.prod(dshape) # Load pretrained params gens = dcnn.get_module("g0", dshape, ctx) gens.load_params("model/0020-0000088-sketch.params") #Testing sketch_data = [] sketch_np = data_processing.PreprocessSketchImage(img, long_edge) # mySketch = mx.nd.array(sketch_np) # sketch_data.append(mySketch) gens.forward(mx.io.DataBatch([sketch_data[-1]], [0]), is_train=False) new_img = gens.get_outputs()[0] # output = data_processing.SaveImage(new_img.asnumpy(),"output/test/out_%s" % file_sketch[training+idx]) output = data_processing.SaveImage(new_img.asnumpy(), "output/test/out_%s") cv2.imwrite("output.jpg", output)
import gen_v4 dshape = (1, 3, 480, 640) clip_norm = 1.0 * np.prod(dshape) model_prefix = "./model/" ctx = mx.gpu(0) # generator gens = [ gen_v4.get_module("g0", dshape, ctx), gen_v3.get_module("g1", dshape, ctx), gen_v3.get_module("g2", dshape, ctx), gen_v4.get_module("g3", dshape, ctx) ] for i in range(len(gens)): gens[i].load_params("./model/%d/v3_0002-0026000.params" % i) content_np = data_processing.PreprocessContentImage("../IMG_4343.jpg", min(dshape[2:]), dshape) data = [mx.nd.array(content_np)] for i in range(len(gens)): gens[i].forward(mx.io.DataBatch([data[-1]], [0]), is_train=False) new_img = gens[i].get_outputs()[0] data.append(new_img.copyto(mx.cpu())) data_processing.SaveImage(new_img.asnumpy(), "out_%d.jpg" % i) import os os.system("rm -rf out.zip") os.system("zip out.zip out_*")
grad = mx.nd.zeros(sketch_nd.shape) tv_grad_executor = get_tv_grad_executor(gens.get_outputs()[0], ctx, args.tv_weight) tv_grad_executor.forward() # new_grad grad[:] += loss_grad_array + tv_grad_executor.outputs[0].copyto(mx.cpu()) gnorm = mx.nd.norm(grad).asscalar() if gnorm > clip_norm: #logging.info("Epoch[%d] Image[%d]: Data Grad: %.5f" %\ # (i, idx, (gnorm / clip_norm))) grad[:] *= clip_norm / gnorm tot_gnorm += gnorm / clip_norm gens.backward([grad]) gens.update() new_img = gens.get_outputs()[0] if (idx+1) % num_sketch == 0: ave_gnorm = tot_gnorm / num_sketch logging.info("Epoch[%d]: Epoch Data Grad: %.5f" % (i+1, ave_gnorm)) if ((i+1) % args.save_epochs == 0 and (idx+1) % num_sketch == 0): data_processing.SaveImage(new_img.asnumpy(), 'output/train/out_'+str(i+1)+'_'+file_sketch[idx]) gens.save_params("model/%04d-%07d-sketch.params" % ((i+1), (idx+1))) logging.info("Save Params : %04d-%07d-sketch.params" % ((i+1), (idx+1))) logging.info("Time elapsed for one epoch in seconds: " + str(time.time()-tic))
file_sketch.sort() training = int(0.7 * len(file_sketch)) # 70% num_sketch = int(len(file_sketch) - training) # 30% logging.info("Num of sketches: %d" % num_sketch) # Init sketch sketch_np = data_processing.PreprocessSketchImage( dir_sketch + file_sketch[training], long_edge) logging.info("load the sketch image, size = %s", sketch_np.shape[2:]) dshape = sketch_np.shape clip_norm = 0.05 * np.prod(dshape) # Load pretrained params gens = dcnn.get_module("g0", dshape, ctx) gens.load_params("model/0265-0011975-sketch-vgg16.params") # Testing logging.info('Start testing arguments %s', args) for idx in range(num_sketch): # Load sketch sketch_data = [] path_sketch = dir_sketch + file_sketch[training + idx] sketch_np = data_processing.PreprocessSketchImage(path_sketch, long_edge) mySketch = mx.nd.array(sketch_np) sketch_data.append(mySketch) gens.forward(mx.io.DataBatch([sketch_data[-1]], [0]), is_train=False) new_img = gens.get_outputs()[0] data_processing.SaveImage( new_img.asnumpy(), "output/test/out_%s" % file_sketch[training + idx])