File size: 5,541 Bytes
ca6cab8
8ff8f45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
841fed0
8ff8f45
841fed0
8ff8f45
 
 
 
 
 
 
 
841fed0
8ff8f45
 
 
 
 
 
 
 
fdbe404
8ff8f45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca6cab8
8ff8f45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca6cab8
8ff8f45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca6cab8
8ff8f45
 
 
 
 
 
 
 
fdbe404
 
 
 
 
 
 
 
 
8ff8f45
 
 
 
 
 
ca6cab8
8ff8f45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "view-in-github"
   },
   "source": [
    "<a href=\"https://colab.research.google.com/github/soumik12345/enhance-me/blob/mirnet/notebooks/enhance_me_train.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "1JryaVhtBHij",
    "outputId": "97ee6a4a-2479-4124-e96a-f0a792bdec46"
   },
   "outputs": [],
   "source": [
    "!git clone https://github.com/soumik12345/enhance-me -b mirnet\n",
    "!pip install -qqq wandb streamlit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "G_c4VtXWHR5l"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "\n",
    "sys.path.append(\"./enhance-me\")\n",
    "\n",
    "from PIL import Image\n",
    "from enhance_me import commons\n",
    "from enhance_me.mirnet import MIRNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "ZpBHbYaMIqP_"
   },
   "outputs": [],
   "source": [
    "# @title MIRNet Train Configs\n",
    "\n",
    "experiment_name = \"lol_dataset_256\"  # @param {type:\"string\"}\n",
    "image_size = 128  # @param {type:\"integer\"}\n",
    "dataset_label = \"lol\"  # @param [\"lol\"]\n",
    "apply_random_horizontal_flip = True  # @param {type:\"boolean\"}\n",
    "apply_random_vertical_flip = True  # @param {type:\"boolean\"}\n",
    "apply_random_rotation = True  # @param {type:\"boolean\"}\n",
    "wandb_api_key = \"\"  # @param {type:\"string\"}\n",
    "val_split = 0.1  # @param {type:\"slider\", min:0.1, max:1.0, step:0.1}\n",
    "batch_size = 4  # @param {type:\"integer\"}\n",
    "num_recursive_residual_groups = 3  # @param {type:\"slider\", min:1, max:5, step:1}\n",
    "num_multi_scale_residual_blocks = 2  # @param {type:\"slider\", min:1, max:5, step:1}\n",
    "learning_rate = 1e-4  # @param {type:\"number\"}\n",
    "epsilon = 1e-3  # @param {type:\"number\"}\n",
    "epochs = 50  # @param {type:\"slider\", min:10, max:100, step:5}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 52
    },
    "id": "IVRoedqBIMuH",
    "outputId": "53ca5beb-871a-4ec3-b757-173e09a15331"
   },
   "outputs": [],
   "source": [
    "mirnet = MIRNet(\n",
    "    experiment_name=experiment_name,\n",
    "    wandb_api_key=None if wandb_api_key == \"\" else wandb_api_key,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "O66Iwzx8IsGh",
    "outputId": "0b6f1683-65d1-4737-a32f-d36b331d2bc2"
   },
   "outputs": [],
   "source": [
    "mirnet.build_datasets(\n",
    "    image_size=image_size,\n",
    "    dataset_label=dataset_label,\n",
    "    apply_random_horizontal_flip=apply_random_horizontal_flip,\n",
    "    apply_random_vertical_flip=apply_random_vertical_flip,\n",
    "    apply_random_rotation=apply_random_rotation,\n",
    "    val_split=val_split,\n",
    "    batch_size=batch_size,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "tsfKrBCsL_Bb"
   },
   "outputs": [],
   "source": [
    "mirnet.build_model(\n",
    "    num_recursive_residual_groups=num_recursive_residual_groups,\n",
    "    num_multi_scale_residual_blocks=num_multi_scale_residual_blocks,\n",
    "    learning_rate=learning_rate,\n",
    "    epsilon=epsilon,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "y3L9wlpkNziL",
    "outputId": "5149f0e7-91f4-450f-c43a-1b6028692bbc"
   },
   "outputs": [],
   "source": [
    "history = mirnet.train(epochs=epochs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mirnet.load_weights(os.path.join(mirnet.experiment_name, \"weights.h5\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "background_save": true
    },
    "id": "daFKbgBkiyzc"
   },
   "outputs": [],
   "source": [
    "for index, low_image_file in enumerate(mirnet.test_low_images):\n",
    "    original_image = Image.open(low_image_file)\n",
    "    enhanced_image = mirnet.infer(original_image)\n",
    "    ground_truth = Image.open(mirnet.test_enhanced_images[index])\n",
    "    commons.plot_results(\n",
    "        [original_image, ground_truth, ground_truth],\n",
    "        [\"Original Image\", \"Ground Truth\", \"Enhanced Image\"],\n",
    "        (18, 18),\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "dO-IbNQHkB3R"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "authorship_tag": "ABX9TyN4LuJh6kWhbqxzA5s9sp7k",
   "collapsed_sections": [],
   "include_colab_link": true,
   "machine_shape": "hm",
   "name": "enhance-me-train.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}