Spaces:
Runtime error
Runtime error
File size: 12,035 Bytes
31a913a |
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Video Model Training"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### NOTES: \n",
"* It's assumed that there's a pretrained generator from the ColorizeTrainingStable notebook available at the specified path.\n",
"* This is \"NoGAN\" based training, described in the DeOldify readme."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#NOTE: This must be the first call in order to work properly!\n",
"from deoldify import device\n",
"from deoldify.device_id import DeviceId\n",
"#choices: CPU, GPU0...GPU7\n",
"device.set(device=DeviceId.GPU0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import fastai\n",
"from fastai import *\n",
"from fastai.vision import *\n",
"from fastai.callbacks.tensorboard import *\n",
"from fastai.vision.gan import *\n",
"from deoldify.generators import *\n",
"from deoldify.critics import *\n",
"from deoldify.dataset import *\n",
"from deoldify.loss import *\n",
"from deoldify.save import *\n",
"from deoldify.augs import noisify \n",
"from PIL import Image, ImageDraw, ImageFont\n",
"from PIL import ImageFile"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"path = Path('data/imagenet/ILSVRC/Data/CLS-LOC')\n",
"path_hr = path\n",
"path_lr = path/'bandw'\n",
"\n",
"proj_id = 'VideoModel'\n",
"gen_name = proj_id + '_gen'\n",
"pre_gen_name = gen_name + '_0'\n",
"crit_name = proj_id + '_crit'\n",
"\n",
"name_gen = proj_id + '_image_gen'\n",
"path_gen = path/name_gen\n",
"\n",
"TENSORBOARD_PATH = Path('data/tensorboard/' + proj_id)\n",
"\n",
"nf_factor = 2\n",
"xtra_tfms=[noisify(p=0.8)]\n",
"pct_start = 1e-8"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_data(bs:int, sz:int, keep_pct:float):\n",
" return get_colorize_data(sz=sz, bs=bs, crappy_path=path_lr, good_path=path_hr, \n",
" random_seed=None, keep_pct=keep_pct, xtra_tfms=xtra_tfms)\n",
"\n",
"def get_crit_data(classes, bs, sz):\n",
" src = ImageList.from_folder(path, include=classes, recurse=True).split_by_rand_pct(0.1, seed=42)\n",
" ll = src.label_from_folder(classes=classes)\n",
" data = (ll.transform(get_transforms(max_zoom=2.), size=sz)\n",
" .databunch(bs=bs).normalize(imagenet_stats))\n",
" return data\n",
"\n",
"def create_training_images(fn,i):\n",
" dest = path_lr/fn.relative_to(path_hr)\n",
" dest.parent.mkdir(parents=True, exist_ok=True)\n",
" img = PIL.Image.open(fn).convert('LA').convert('RGB')\n",
" img.save(dest) \n",
" \n",
"def save_preds(dl):\n",
" i=0\n",
" names = dl.dataset.items\n",
" \n",
" for b in dl:\n",
" preds = learn_gen.pred_batch(batch=b, reconstruct=True)\n",
" for o in preds:\n",
" o.save(path_gen/names[i].name)\n",
" i += 1\n",
" \n",
"def save_gen_images():\n",
" if path_gen.exists(): shutil.rmtree(path_gen)\n",
" path_gen.mkdir(exist_ok=True)\n",
" data_gen = get_data(bs=bs, sz=sz, keep_pct=0.085)\n",
" save_preds(data_gen.fix_dl)\n",
" PIL.Image.open(path_gen.ls()[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create black and white training images"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Only runs if the directory isn't already created."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if not path_lr.exists():\n",
" il = ImageList.from_folder(path_hr)\n",
" parallel(create_training_images, il.items)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finetune Generator With Noise Augmented Images."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### This helps the generator better deal with noisy/grainy video (which is pretty normal)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bs=8\n",
"sz=192\n",
"keep_pct=0.25"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_gen = get_data(bs=bs, sz=sz, keep_pct=keep_pct)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_gen = gen_learner_wide(data=data_gen, gen_loss=FeatureLoss(), nf_factor=nf_factor)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_gen.callback_fns.append(partial(ImageGenTensorboardWriter, base_dir=TENSORBOARD_PATH, name='GenPre'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_gen = learn_gen.load(pre_gen_name, with_opt=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_gen.unfreeze()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_gen.fit_one_cycle(1, pct_start=pct_start, max_lr=slice(5e-8,5e-5))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_gen.save(pre_gen_name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Repeatable GAN Cycle"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### NOTE\n",
"Best results so far have been based only doing a single run of the cells below (otherwise glitches are introduced that are visible in video). "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"old_checkpoint_num = 0\n",
"checkpoint_num = old_checkpoint_num + 1\n",
"gen_old_checkpoint_name = gen_name + '_' + str(old_checkpoint_num)\n",
"gen_new_checkpoint_name = gen_name + '_' + str(checkpoint_num)\n",
"crit_old_checkpoint_name = crit_name + '_' + str(old_checkpoint_num)\n",
"crit_new_checkpoint_name= crit_name + '_' + str(checkpoint_num)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Save Generated Images"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bs=8\n",
"sz=192"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_gen = gen_learner_wide(data=data_gen, gen_loss=FeatureLoss(), nf_factor=nf_factor).load(gen_old_checkpoint_name, with_opt=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"save_gen_images()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pretrain Critic"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bs=16\n",
"sz=192"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_gen=None\n",
"gc.collect()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_crit = get_crit_data([name_gen, 'test'], bs=bs, sz=sz)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_crit.show_batch(rows=3, ds_type=DatasetType.Train, imgsize=3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_critic = colorize_crit_learner(data=data_crit, nf=256).load(crit_old_checkpoint_name, with_opt=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_critic.callback_fns.append(partial(LearnerTensorboardWriter, base_dir=TENSORBOARD_PATH, name='CriticPre'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_critic.fit_one_cycle(4, 1e-4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_critic.save(crit_new_checkpoint_name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### GAN"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_crit=None\n",
"learn_gen=None\n",
"gc.collect()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lr=5e-6\n",
"sz=192\n",
"bs=5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_crit = get_crit_data([name_gen, 'test'], bs=bs, sz=sz)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_crit = colorize_crit_learner(data=data_crit, nf=256).load(crit_new_checkpoint_name, with_opt=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn_gen = gen_learner_wide(data=data_gen, gen_loss=FeatureLoss(), nf_factor=nf_factor).load(gen_old_checkpoint_name, with_opt=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"switcher = partial(AdaptiveGANSwitcher, critic_thresh=0.65)\n",
"learn = GANLearner.from_learners(learn_gen, learn_crit, weights_gen=(1.0,1.5), show_img=False, switcher=switcher,\n",
" opt_func=partial(optim.Adam, betas=(0.,0.9)), wd=1e-3)\n",
"learn.callback_fns.append(partial(GANDiscriminativeLR, mult_lr=5.))\n",
"learn.callback_fns.append(partial(GANTensorboardWriter, base_dir=TENSORBOARD_PATH, name='GanLearner', visual_iters=100, stats_iters=10, loss_iters=1))\n",
"learn.callback_fns.append(partial(GANSaveCallback, learn_gen=learn_gen, filename=gen_new_checkpoint_name, save_iters=100))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Instructions: \n",
"Find the checkpoint just before where glitches start to be introduced. So far this has been found at the point of iterating through 1.4% of the data when using learning rate of 1e-5, and at 2.2% of the data for 5e-6."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learn.data = get_data(sz=sz, bs=bs, keep_pct=0.03)\n",
"learn_gen.freeze_to(-1)\n",
"learn.fit(1,lr)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|