NDugar commited on
Commit
f8c560a
1 Parent(s): 9b51548

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -48
app.py CHANGED
@@ -25,55 +25,24 @@ from fastai.learner import load_learner
25
  model = load_learner(
26
  hf_hub_download("NDugar/horse_to_zebra_cycle_GAN", "h2z-85epoch.pth")
27
  )
28
- model.eval()
29
- def get_model():
30
- return model
31
- def adjust_image_for_model(img):
32
- logger.info(f"Image Height: {img.height}, Image Width: {img.width}")
33
- return img
34
- def inference(img, style):
35
- img = adjust_image_for_model(img)
36
- input_image = img.convert(COLOUR_MODEL)
37
- input_image = np.asarray(input_image)
38
- input_image = input_image[:, :, [2, 1, 0]]
39
- input_image = transforms.ToTensor()(input_image).unsqueeze(0)
40
- input_image = -1 + 2 * input_image
41
 
42
- if enable_gpu:
43
- logger.info(f"CUDA found. Using GPU.")
44
- input_image = Variable(input_image).cuda()
45
- else:
46
- logger.info(f"CUDA not found. Using CPU.")
47
- input_image = Variable(input_image).float()
48
- model = get_model()
49
- output_image = model(input_image)
50
- output_image = output_image[0]
51
- # BGR -> RGB
52
- output_image = output_image[[2, 1, 0], :, :]
53
- output_image = output_image.data.cpu().float() * 0.5 + 0.5
54
 
55
- return transforms.ToPILImage()(output_image)
56
- title = "Horse 2 Zebra GAN"
57
- description = "Gradio Demo for CycleGAN"
58
-
59
- gr.Interface(
60
- fn=inference,
61
- inputs=[
62
- gr.inputs.Image(
63
- type="pil",
64
- label="Input Photo",
65
- ),
66
- ],
67
- outputs=gr.outputs.Image(
68
- type="pil",
69
- label="Output Image",
70
- ),
71
- title=title,
72
- description=description,
73
- article=article,
74
- examples=examples,
75
- allow_flagging="never",
76
- allow_screenshot=False,
77
- ).launch(enable_queue=True)
78
 
 
 
 
 
 
 
 
79
 
 
 
25
  model = load_learner(
26
  hf_hub_download("NDugar/horse_to_zebra_cycle_GAN", "h2z-85epoch.pth")
27
  )
28
+ def generate_img(img_path):
29
+ img = tf.io.read_file(img_path)
30
+ img = tf.image.decode_png(img)
31
+ img = tf.expand_dims(img, axis=0)
32
+ img = preprocess_test_image(img)
33
+ prediction = model(img, training=False)[0].numpy()
34
+ return prediction
 
 
 
 
 
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ image = gr.inputs.Image(type="filepath")
38
+ op = gr.outputs.Image(type="numpy")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
+ iface = gr.Interface(
41
+ generate_img,
42
+ image,
43
+ op,
44
+ title="CycleGAN-using UPIT",
45
+ description='CycleGAN model using Horse to Zebra using UPIT - https://github.com/tmabraham/UPIT'
46
+ )
47
 
48
+ iface.launch(cache_examples=False)