ismot GIanlucaRub commited on
Commit
c3ae2a6
·
0 Parent(s):

Duplicate from GIanlucaRub/DoubleResolution

Browse files

Co-authored-by: Gianluca Ruberto <[email protected]>

Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +143 -0
  4. requirements.txt +2 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: DoubleResolution
3
+ emoji: 🐢
4
+ colorFrom: red
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 3.15.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: GIanlucaRub/DoubleResolution
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from math import ceil
4
+ from huggingface_hub import from_pretrained_keras
5
+
6
+ model = from_pretrained_keras("GIanlucaRub/doubleResFinal")
7
+ def double_res(input_image):
8
+ input_height = input_image.shape[0]
9
+ input_width = input_image.shape[1]
10
+ height = ceil(input_height/128)
11
+ width = ceil(input_width/128)
12
+ expanded_input_image = np.zeros((128*height, 128*width, 3), dtype=np.uint8)
13
+ np.copyto(expanded_input_image[0:input_height, 0:input_width], input_image)
14
+
15
+ output_image = np.zeros((128*height*2, 128*width*2, 3), dtype=np.float32)
16
+
17
+ to_predict = []
18
+ for i in range(height):
19
+ for j in range(width):
20
+ temp_slice = expanded_input_image[i *
21
+ 128:(i+1)*128, j*128:(j+1)*128]/255
22
+ to_predict.append(temp_slice)
23
+
24
+ # removing inner borders
25
+
26
+ for i in range(height):
27
+ for j in range(width):
28
+ if i != 0 and j != 0 and i != height-1 and j != width-1:
29
+ right_slice = expanded_input_image[i *
30
+ 128:(i+1)*128, (j+1)*128-64:(j+1)*128+64]/255
31
+ to_predict.append(right_slice)
32
+
33
+
34
+ left_slice = expanded_input_image[i *
35
+ 128:(i+1)*128, j*128-64:(j)*128+64]/255
36
+ to_predict.append(left_slice)
37
+
38
+
39
+ upper_slice = expanded_input_image[(
40
+ i+1)*128-64:(i+1)*128+64, j*128:(j+1)*128]/255
41
+ to_predict.append(upper_slice)
42
+
43
+
44
+ lower_slice = expanded_input_image[i *
45
+ 128-64:i*128+64, j*128:(j+1)*128]/255
46
+ to_predict.append(lower_slice)
47
+ # removing angles
48
+
49
+ lower_right_slice = expanded_input_image[i *
50
+ 128-64:i*128+64, (j+1)*128-64:(j+1)*128+64]/255
51
+ to_predict.append(lower_right_slice)
52
+
53
+ lower_left_slice = expanded_input_image[i *
54
+ 128-64:i*128+64, j*128-64:j*128+64]/255
55
+ to_predict.append(lower_left_slice)
56
+
57
+ # predicting all images at once
58
+ completed = False
59
+ n = 16
60
+ while not completed:
61
+ try:
62
+ print("attempting with "+ str(n))
63
+ predicted = model.predict(np.array(to_predict),batch_size = n)
64
+ completed = True
65
+ print("completed with "+ str(n))
66
+ except:
67
+ print("attempt with " + str(n) + " failed")
68
+ n += -1
69
+ if n <= 0:
70
+ n = 1
71
+ counter = 0
72
+ for i in range(height):
73
+ for j in range(width):
74
+ np.copyto(output_image[i*256:(i+1)*256, j *
75
+ 256:(j+1)*256], predicted[counter])
76
+ counter+=1
77
+
78
+
79
+
80
+ for i in range(height):
81
+ for j in range(width):
82
+ if i != 0 and j != 0 and i != height-1 and j != width-1:
83
+ right_upsampled_slice = predicted[counter]
84
+ counter+=1
85
+ resized_right_slice = right_upsampled_slice[64:192, 64:192]
86
+ np.copyto(output_image[i*256+64:(i+1)*256-64,
87
+ (j+1)*256-64:(j+1)*256+64], resized_right_slice)
88
+
89
+
90
+
91
+
92
+ left_upsampled_slice = predicted[counter]
93
+ counter+=1
94
+ resized_left_slice = left_upsampled_slice[64:192, 64:192]
95
+ np.copyto(output_image[i*256+64:(i+1)*256-64,
96
+ j*256-64:j*256+64], resized_left_slice)
97
+
98
+
99
+
100
+ upper_upsampled_slice = predicted[counter]
101
+ counter+=1
102
+ resized_upper_slice = upper_upsampled_slice[64:192, 64:192]
103
+ np.copyto(output_image[(i+1)*256-64:(i+1)*256+64,
104
+ j*256+64:(j+1)*256-64], resized_upper_slice)
105
+
106
+
107
+
108
+ lower_upsampled_slice = predicted[counter]
109
+ counter+=1
110
+ resized_lower_slice = lower_upsampled_slice[64:192, 64:192]
111
+ np.copyto(output_image[i*256-64:i*256+64,
112
+ j*256+64:(j+1)*256-64], resized_lower_slice)
113
+
114
+
115
+
116
+ lower_right_upsampled_slice = predicted[counter]
117
+ counter+=1
118
+ resized_lower_right_slice = lower_right_upsampled_slice[64:192, 64:192]
119
+ np.copyto(output_image[i*256-64:i*256+64, (j+1)
120
+ * 256-64:(j+1)*256+64], resized_lower_right_slice)
121
+
122
+
123
+ lower_left_upsampled_slice = predicted[counter]
124
+ counter+=1
125
+ resized_lower_left_slice = lower_left_upsampled_slice[64:192, 64:192]
126
+ np.copyto(
127
+ output_image[i*256-64:i*256+64, j*256-64:j*256+64], resized_lower_left_slice)
128
+
129
+ resized_output_image = output_image[0:input_height*2, 0:input_width*2]
130
+ return resized_output_image
131
+
132
+ demo = gr.Interface(
133
+ fn=double_res,
134
+ title="Double picture resolution",
135
+ description="Upload a picture and get the horizontal and vertical resolution doubled (4x pixels)",
136
+ allow_flagging="never",
137
+ inputs=[
138
+ gr.inputs.Image(type="numpy")
139
+ ],
140
+ outputs=gr.Image(type="numpy"))
141
+
142
+ demo.launch()
143
+
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ numpy
2
+ tensorflow