Giter Site home page Giter Site logo

mathiasgruber / pconv-keras Goto Github PK

View Code? Open in Web Editor NEW
903.0 28.0 252.0 66.55 MB

Unofficial implementation of "Image Inpainting for Irregular Holes Using Partial Convolutions". Try at: www.fixmyphoto.ai

License: MIT License

Jupyter Notebook 99.73% Python 0.27%
deep-learning ai inpainting nvidia keras tensorflow python

pconv-keras's Introduction

Partial Convolutions for Image Inpainting using Keras

Keras implementation of "Image Inpainting for Irregular Holes Using Partial Convolutions", https://arxiv.org/abs/1804.07723. A huge shoutout the authors Guilin Liu, Fitsum A. Reda, Kevin J. Shih, Ting-Chun Wang, Andrew Tao and Bryan Catanzaro from NVIDIA corporation for releasing this awesome paper, it's been a great learning experience for me to implement the architecture, the partial convolutional layer, and the loss functions.

Dependencies

  • Python 3.6
  • Keras 2.2.4
  • Tensorflow 1.12

How to use this repository

The easiest way to try a few predictions with this algorithm is to go to www.fixmyphoto.ai, where I've deployed it on a serverless React application with AWS lambda functions handling inference.

If you want to dig into the code, the primary implementations of the new PConv2D keras layer as well as the UNet-like architecture using these partial convolutional layers can be found in libs/pconv_layer.py and libs/pconv_model.py, respectively - this is where the bulk of the implementation can be found. Beyond this I've set up four jupyter notebooks, which details the several steps I went through while implementing the network, namely:

Step 1: Creating random irregular masks
Step 2: Implementing and testing the implementation of the PConv2D layer
Step 3: Implementing and testing the UNet architecture with PConv2D layers
Step 4: Training & testing the final architecture on ImageNet
Step 5: Simplistic attempt at predicting arbitrary image sizes through image chunking

Pre-trained weights

I've ported the VGG16 weights from PyTorch to keras; this means the 1/255. pixel scaling can be used for the VGG16 network similarly to PyTorch.

Training on your own dataset

You can either go directly to step 4 notebook, or alternatively use the CLI (make sure to download the converted VGG16 weights):

python main.py \
    --name MyDataset \
    --train TRAINING_PATH \
    --validation VALIDATION_PATH \
    --test TEST_PATH \
    --vgg_path './data/logs/pytorch_to_keras_vgg16.h5'

Implementation details

Details of the implementation are in the paper itself, however I'll try to summarize some details here.

Mask Creation

In the paper they use a technique based on occlusion/dis-occlusion between two consecutive frames in videos for creating random irregular masks - instead I've opted for simply creating a simple mask-generator function which uses OpenCV to draw some random irregular shapes which I then use for masks. Plugging in a new mask generation technique later should not be a problem though, and I think the end results are pretty decent using this method as well.

Partial Convolution Layer

A key element in this implementation is the partial convolutional layer. Basically, given the convolutional filter W and the corresponding bias b, the following partial convolution is applied instead of a normal convolution:

where ⊙ is element-wise multiplication and M is a binary mask of 0s and 1s. Importantly, after each partial convolution, the mask is also updated, so that if the convolution was able to condition its output on at least one valid input, then the mask is removed at that location, i.e.

The result of this is that with a sufficiently deep network, the mask will eventually be all ones (i.e. disappear)

UNet Architecture

Specific details of the architecture can be found in the paper, but essentially it's based on a UNet-like structure, where all normal convolutional layers are replace with partial convolutional layers, such that in all cases the image is passed through the network alongside the mask. The following provides an overview of the architecture.

Loss Function(s)

The loss function used in the paper is kinda intense, and can be reviewed in the paper. In short it includes:

  • Per-pixel losses both for maskes and un-masked regions
  • Perceptual loss based on ImageNet pre-trained VGG-16 (pool1, pool2 and pool3 layers)
  • Style loss on VGG-16 features both for predicted image and for computed image (non-hole pixel set to ground truth)
  • Total variation loss for a 1-pixel dilation of the hole region

The weighting of all these loss terms are as follows:

Training Procedure

Network was trained on ImageNet with a batch size of 1, and each epoch was specified to be 10,000 batches long. Training was furthermore performed using the Adam optimizer in two stages since batch normalization presents an issue for the masked convolutions (since mean and variance is calculated for hole pixels).

Stage 1 Learning rate of 0.0001 for 50 epochs with batch normalization enabled in all layers

Stage 2 Learning rate of 0.00005 for 50 epochs where batch normalization in all encoding layers is disabled.

Training time for shown images was absolutely crazy long, but that is likely because of my poor personal setup. The few tests I've tried on a 1080Ti (with batch size of 4) indicates that training time could be around 10 days, as specified in the paper.

pconv-keras's People

Contributors

mathiasgruber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pconv-keras's Issues

Pretrained models

thanks a lot for sharing yr code.I spent many time to use my device to train.Can you publish your pretrained models.

ValueError: invalid literal for int() with base 10: 'h5'

I'm trying to run (from step 5 ipynb):

from libs.pconv_model import PConvUnet
model = PConvUnet(vgg_weights=None, inference_only=True)
model.load(r"/media/user/PConv-Keras/pconv_imagenet.h5", train_bn=False)

But it fails with ValueError: invalid literal for int() with base 10: 'h5' error:

ValueError                                Traceback (most recent call last)
<ipython-input-11-adc04f0f404a> in <module>
      1 from libs.pconv_model import PConvUnet
      2 model = PConvUnet(vgg_weights=None, inference_only=True)
----> 3 model.load(r"/media/user/PConv-Keras//pconv_imagenet.h5", train_bn=False)

/media/user/PConv-Keras/libs/pconv_model.py in load(self, filepath, train_bn, lr)
    266 
    267         # Load weights into model
--> 268         epoch = int(os.path.basename(filepath).split('.')[1].split('-')[0])
    269         assert epoch > 0, "Could not parse weight file. Should include the epoch"
    270         self.current_epoch = epoch

ValueError: invalid literal for int() with base 10: 'h5'

Over-fitting with data generation

Hi,
thank you for sharing this code. It is rather a general question than an issue.

Do not you think you over-fit if you randomly generate masks on the same images and it happens that with future iterations the net eventually will see the whole image?

I am just curios how you dealt with this problem.

Prediction image becomes abnormal

I use pconv_imagenet as pretrained model

The result abnormally contains blue as shown below.

Koci1t.png

KoccMP.png

Why does it become like that and how to fix it?

preprocess_input for VGG16

You are using VGG16 with imagenet weights, should it using preprocess_input to computing VGG features?

        img = Input(shape=(self.img_rows, self.img_cols, 3))
        # Get the vgg network from Keras applications
        processed=Lambda(preprocess_input, name='Input_Image', input_shape=(self.img_rows, self.img_cols, 3))(img)
        vgg = VGG16(weights="imagenet", include_top=False)

        # Output the first three pooling layers
        vgg.outputs = [vgg.layers[i].output for i in self.vgg_layers]

        # Create model and compile
        model = Model(inputs=img, outputs=vgg(processed))

The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.

when i run:
python main.py --name MyDataset --train C:\Documents\Kaggle\Kaggle-imagenet\ILSVRC\Data\CLS-LOC\train --validation C:\Documents\Kaggle\Kaggle-imagenet\ILSVRC\Data\CLS-LOC -test C:\Documents\Kaggle\pexels --vgg_path 'D:\artAI\model\pytorch_to_keras_vgg16.h5'

error is ocurred:
2019-04-23 14:44:49.128950: I tensorflow/core/common_runtime/bfc_allocator.cc:641] 1 Chunks of size 153363456 totalling 146.26MiB
2019-04-23 14:44:49.134231: I tensorflow/core/common_runtime/bfc_allocator.cc:641] 5 Chunks of size 201326592 totalling 960.00MiB
2019-04-23 14:44:49.138279: I tensorflow/core/common_runtime/bfc_allocator.cc:641] 2 Chunks of size 212413696 totalling 405.15MiB
2019-04-23 14:44:49.148331: I tensorflow/core/common_runtime/bfc_allocator.cc:645] Sum Total of in-use chunks: 4.56GiB
2019-04-23 14:44:49.151895: I tensorflow/core/common_runtime/bfc_allocator.cc:647] Stats:
Limit: 4949881651
InUse: 4894695424
MaxInUse: 4895090944
NumAllocs: 1693
MaxAllocSize: 2529192960

2019-04-23 14:44:49.162031: W tensorflow/core/common_runtime/bfc_allocator.cc:271] ****************************************************************************************************
2019-04-23 14:44:49.168876: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at conv_ops.cc:746 : Resource exhausted: OOM when allocating tensor with shape[3,256,128,128] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
Traceback (most recent call last):
File "c:\Users\flyon.vscode\extensions\ms-python.python-2019.3.6558\pythonFiles\ptvsd_launcher.py", line 45, in
main(ptvsdArgs)
File "c:\Users\flyon.vscode\extensions\ms-python.python-2019.3.6558\pythonFiles\lib\python\ptvsd_main_.py", line 391, in main
run()
File "c:\Users\flyon.vscode\extensions\ms-python.python-2019.3.6558\pythonFiles\lib\python\ptvsd_main_.py", line 272, in run_file
runpy.run_path(target, run_name='main')
File "C:\Users\flyon\AppData\Local\conda\conda\envs\artai\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\flyon\AppData\Local\conda\conda\envs\artai\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\flyon\AppData\Local\conda\conda\envs\artai\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "d:\artAI\PConv-Keras\main.py", line 240, in
TQDMCallback()
File "d:\artAI\PConv-Keras\libs\pconv_model.py", line 254, in fit_generator
*args, **kwargs
File "C:\Users\flyon\AppData\Local\conda\conda\envs\artai\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\flyon\AppData\Local\conda\conda\envs\artai\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
initial_epoch=initial_epoch)
File "C:\Users\flyon\AppData\Local\conda\conda\envs\artai\lib\site-packages\keras\engine\training_generator.py", line 217, in fit_generator
class_weight=class_weight)
File "C:\Users\flyon\AppData\Local\conda\conda\envs\artai\lib\site-packages\keras\engine\training.py", line 1217, in train_on_batch
outputs = self.train_function(ins)
File "C:\Users\flyon\AppData\Local\conda\conda\envs\artai\lib\site-packages\keras\backend\tensorflow_backend.py", line 2715, in call
return self._call(inputs)
File "C:\Users\flyon\AppData\Local\conda\conda\envs\artai\lib\site-packages\keras\backend\tensorflow_backend.py", line 2675, in _call
fetched = self._callable_fn(*array_vals)
File "C:\Users\flyon\AppData\Local\conda\conda\envs\artai\lib\site-packages\tensorflow\python\client\session.py", line 1439, in call
run_metadata_ptr)
File "C:\Users\flyon\AppData\Local\conda\conda\envs\artai\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in exit
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[3,256,128,128] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
[[{{node loss_1/outputs_img_loss/model_1_2/vgg16/block3_conv3/convolution}} = Conv2D[T=DT_FLOAT, _class=["loc:@train...kpropInput"], data_format="NCHW", dilations=[1, 1, 1, 1], padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](loss_1/outputs_img_loss/model_1_2/vgg16/block3_conv2/Relu, block3_conv3/kernel/read)]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

     [[{{node loss_1/mul/_1041}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_8425_loss_1/mul", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

Question - grayscale image example

I am attempting to use transfer learning techniques to apply this model to a similar problem in the game of battleships. I am using the model to predict the positions of ships in a partially visible board (as in the game of battleships). The board of battleships is a 10 by 10 2d array with numbers 1 through 5 indicating which ship is placed in which cell of the board and 0 for water. In a sense the board is the same as a 10 by 10 grayscale image which I believe this model will be able to handle.

Could you provide an example using grayscale images (preferably size 10x10)?

fix an eye that is completely obscured

Screenshot from 2019-04-28 17-19-42
Screenshot from 2019-04-28 17-25-16

I use your easiest way to inpaint a face that blocks one eye in your link ''www.fixmyphoto.ai'. Why can't it fix an eye that is completely obscured? Is it because it is not trained on this data set? But it can fix small occlusion like low picture.

ValueError: expects 3 weights, but the saved weights have 2 elements.

I ran the code successfully on my laptop with 1 GPU, however, while I try to try on remote workstaton with NVIDIA Tesla P100 GPU . it does not work. anyone can help me? thanks.

Traceback (most recent call last):
File "/home/irlts/nianyi/PConv-Keras-master/main.py", line 210, in
model.load(args.checkpoint)
File "/home/irlts/nianyi/PConv-Keras-master/libs/pconv_model.py", line 271, in load
self.model.load_weights(filepath)
File "/localscratch/irlts.21223938.0/env/lib/python3.7/site-packages/keras/engine/saving.py", line 492, in load_wrapper
return load_function(*args, **kwargs)
File "/localscratch/irlts.21223938.0/env/lib/python3.7/site-packages/keras/engine/network.py", line 1230, in load_weights
f, self.layers, reshape=reshape)
File "/localscratch/irlts.21223938.0/env/lib/python3.7/site-packages/keras/engine/saving.py", line 1235, in load_weights_from_hdf5_group
' elements.')
ValueError: Layer #0 (named "p_conv2d_17" in the current model) was found to correspond to layer p_conv2d_49 in the save file. However the new layer p_conv2d_17 expects 3 weights, but the saved weights have 2 elements.

training for my own dataset

thanks alot for sharing yr code.
how can i use it to train on my own dataset ? also i have a question regarding used loss function, from wt i understand of yr code is that u only used root mean sqaure loss, is that correct?
yr help is really appropriated

Error when training in notebook with different image size

Hi! I'm trying to train the model on my dataset in notebook #4 with pictures of size 288x288. I have changed image sizes here:

`

Create training generator

train_datagen = AugmentingDataGenerator(
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
rescale=1./255,
horizontal_flip=True
)
train_generator = train_datagen.flow_from_directory(
TRAIN_DIR,
MaskGenerator(288, 288, 3),
target_size=(288, 288),
batch_size=BATCH_SIZE
)

Create validation generator

val_datagen = AugmentingDataGenerator(rescale=1./255)
val_generator = val_datagen.flow_from_directory(
VAL_DIR,
MaskGenerator(288, 288, 3),
target_size=(288, 288),
batch_size=20,
classes=['val'],
seed=42
)

Create testing generator

test_datagen = AugmentingDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
TEST_DIR,
MaskGenerator(288, 288, 3),
target_size=(288, 288),
batch_size=20,
seed=42
)`

but receiving the following error:

ValueError: Error when checking input: expected inputs_img to have shape (512, 512, 3) but got array with shape (288, 288, 3)

in cell #44 when starting to train.

Is there a way to train on a different size or only 512x512?

Is the workflow identical?

I try to fill image with holes on https://www.fixmyphoto.ai/ and it performs well. However when I try the code in notebook (including step 4 and 5), the performance can not keep up with it. Hence, I wonder the workflow between them is identical? I use the released pre-trained imagenet model.

My confusion was solved after Mathias confirms all the workflow are identical.

Question: Is masking the training data needed?

Not sure if it would be much of an optimization, but I noticed the current code produces masked versions of the images for training, and I'm not sure it is a needed step.

Since the partial convolution will use the mask to zero out any contribution from masked pixels, would there be any impact to using the ground truth image? And if it is possible, would there be any benefit?

questions about step3 training on single image

@MathiasGruber Thank you very much for your excellent code! I have come up against some problems when running your code in Step 3, about training UNet on single image. I've downloaded your pretrained VGG weights and put it in model. However, I found the loss decrease very slow, and the predicted image seems not right.

Epoch 1/10
2000/2000 [==============================] - 1239s 619ms/step - loss: 5.1134 - PSNR: 11.9789
1

Epoch 2/10
2000/2000 [==============================] - 1226s 613ms/step - loss: 2.8002 - PSNR: 16.5682
2

Epoch 3/10
2000/2000 [==============================] - 1231s 616ms/step - loss: 2.2371 - PSNR: 19.1468
3

Is there some wrong with my implementation?

How do I start training from scratch?

Sorry if this is a stupid question, I'm a newbie. I've tried reading the notebooks and looking at 'Issues', but I don't quite get how to get this thing to work. Here are some issues I'm facing:

  1. If I don't want to use pre-trained model and I want to start training from nothing, what should I change from the codes? Are the pre trained models requirements to run the program?
  2. What kind of pictures should I put into training, validation, and testing sets?
  3. If I want the program to predict an image (e.g. removing people from a photo), what do I do? Do I just use photo editing software to highlight people with a brush and run it on Step5? If so, what color?

Thank you

Could you provide the pretrained weights?

Hi MathiasGruber,
Firstly I am kindly to thank you for your awesome contribution, especially making them as step by step process.
I have went through the source code, and as you have said that it is going to take time to train the model, could you provide your pretrained weights such as these files:

50_weights_2018-06-01-16-41-43.h5
150_weights_2018-06-26-22-19-32.h5
170_weights_2018-06-28-15-00-38.h5

in "Step4 - Imagenet Training" file
Thank you again 👍 🥇

Predicted image becomes white

Problem

Currently, I am creating a model using images in my house for learning images.
The original model has not changed.
When predicting images, it becomes white like the image below.
I changed the loss function L6 (loss_tv) from 0.1 to 1, but it was ineffective.

What I changed

libs/pconv_model.py in loss_total func

return l1 + 6*l2 + 0.05*l3 + 120*(l4+l5) + 1.0*l6

How to predict

Input image examples

mask image
Screen Shot 2019-03-10 at 11 18 21
In order to avoid recognizing it as a mask when there is a pure white color in the original image, the whole is grayed out.

original color image
Screen Shot 2019-03-10 at 11 18 09

Code

model_rootpath = '/mnt/PConv-Keras/'
model = PConvUnet(weight_filepath='data/model/')
model.load(
    '{}/01/weight/13_weights_2019-03-09-00-20-20.h5'.format(model_rootpath),
    train_bn=False,
    lr=0.00005
)

SAVE_IMG_ROOTPATH = '/mnt/PConv-Keras/predicted_imgs'
CASE_NAME         = 'CASE-01'
GPU_SIZE          = 'GPU-1'
BATCH_SIZE        = 'Batch-07'
EPOCH             = 'Epoch-13'
LEARNING_TIME     = 'Time-200h'

# Create dir
#   ex) CASE-01_GPU-4_Batch-27_Epoch-37_Time-46h
save_filename = ('_').join((CASE_NAME, GPU_SIZE, BATCH_SIZE, EPOCH, LEARNING_TIME))
dir_name = datetime.now().strftime('%Y%m%d_%H%M%S')
save_dir_path = SAVE_IMG_ROOTPATH + '/' + dir_name
os.makedirs(save_dir_path)

start = time.time()
for i in range(6):
    # Load masked image and Create mask image(black and white)
    masked_img = cv2.imread('{0}/input_imgs/img_{1:02d}_mask.jpg'.format(SAVE_IMG_ROOTPATH, i))    
    masked_img = cv2.resize(masked_img, (cst.IMG_WIDTH, cst.IMG_HEIGHT))
    masked_img = cv2.cvtColor(masked_img, cv2.COLOR_BGR2RGB)
    
    # Create mask image
    mask = np.concatenate([((np.sum(masked_img, axis = -1) > 200 * 3) * 255)[..., np.newaxis]] * 3, axis = -1)
    mask_inv_img = 255 - mask

    # Load original color image
    ori_img = cv2.imread('{0}/input_imgs/img_{1:02d}_ori.jpg'.format(SAVE_IMG_ROOTPATH, i))
    ori_img = cv2.resize(ori_img, (cst.IMG_WIDTH, cst.IMG_HEIGHT))
    ori_img = cv2.cvtColor(ori_img, cv2.COLOR_BGR2RGB)

    # Resize for prediction
    model_input_img_tensor = ori_img[np.newaxis, ...]/255.
    model_input_mask_tensor = mask_inv_img[np.newaxis, ...]/255
    
    # Prediction
    model_output = model.predict([model_input_img_tensor, model_input_mask_tensor])
    save_img = Image.fromarray(np.uint8((model_output[0,:,:,:] * 1.)*255))
    save_img.save('{0}/{1}_{2:02d}.jpg'.format(save_dir_path, save_filename, i))

Predicted image

Screen Shot 2019-03-10 at 8 48 00

the training is not complete

thanks alot for sharing your code.
i use the cli to train on my own dataset .
python main.py --name celeba --train ./data/train --test ./data/test --vgg_path ./data/pytorch_to_keras_vgg16.h5

out put on command prompt is :
1111

why no result and training is 0%??
pleas help me and answer to my Question.

How to test on free size of images?

Hi MathiasGruber,
(and Hello guys)
At the training step, you have used 512x512 pixels images.
How can we test free size images ( lets say 640x640 pixels images)?

Thank you.

ImportError: cannot import name 'random_mask'

I'm trying to run "Step5 - Prediction.ipynb" but it fails in first step:

ImportError                               Traceback (most recent call last)
<ipython-input-1-86b0b96f5fd9> in <module>
     14 
     15 from libs.pconv_model import PConvUnet
---> 16 from libs.util import random_mask, ImageChunker
     17 
     18 print(os.getcwd())

ImportError: cannot import name 'random_mask'

PConv Layer compute_shape

I got a question. when input shape is (None,1,512,1) and kernel_size and stride change with (1,*) the pconv output_shape is (None,1,255,1) and conv2d is (None,1,256,1).

There are white spots on the predicted picture corner

@MathiasGruber
when I wrote following code:
`
import os
from copy import deepcopy
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import cv2

if os.path.basename(os.getcwd()) != 'PConv-Keras-new':
os.chdir('..')

from libs.pconv_model import PConvUnet
from libs.util import MaskGenerator, ImageChunker
from libs.pconv_model import PConvUnet
model = PConvUnet(vgg_weights=None, inference_only=True)
model.load(r"D:\artAI\model\weights.26-1.07.h5", train_bn=False)
sample_image_file = os.path.dirname(os.path.realpath(file)) + '\xxt\my_bike_xxt03.jpg'
mask_image_file = os.path.dirname(os.path.realpath(file)) + '\xxt\xxt03.jpg'
img = cv2.imread(sample_image_file)
mask = cv2.imread(mask_image_file)
img = np.array(img)/255
mask = np.array(mask)/255
np.random.seed(16)
chunker = ImageChunker(512, 512, 30)
chunked_images = chunker.dimension_preprocess(deepcopy(img))
chunked_masks = chunker.dimension_preprocess(deepcopy(mask))
pred_imgs = model.predict([chunked_images, chunked_masks])
reconstructed_image = chunker.dimension_postprocess(pred_imgs, img)
reconstructed_image = np.array(reconstructed_image)*255
file_name = 'result{}.jpg'.format(np.random.randint(1,100))
cv2.imwrite(file_name, reconstructed_image)
`
my_bike_xxt03.jpg:
my_bike_xxt03
xxt03.jpg:
xxt03
but the result is:
result42
but when i try https://www.fixmyphoto.ai/, it's correct.Why?
thanks lot

question about the partial convolutional layer

hey, thanks for your codes.
Do you know about the partial convolutional layer in the paper? I have seen your architecture image showed in Readme, but I can't explain that an image which size is 5125123, and is input into a partial layer ,and the output's size is 25625664.The kernel is 77,and the strides is 2.I don't get why 512512 comes to 256*256? Do you have any idea? Thanks!

'MaskGenerator' object has no attribute 'shape'

hello, I'm testing your code with model in readme. And I have a question in step5.
when the code deepcopy mask to chunked_masks in chunked_masks = chunker.dimension_preprocess(deepcopy(mask)), it shows that AttributeError: 'MaskGenerator' object has no attribute 'shape'. Could you please help me to solve the problem? Thank you

Where to download the trained model 'weights.26-1.07.h5'

In Notebook 5, the model is loaded by using:
model.load(r"C:\Users\Mathias Felix Gruber\Documents\GitHub\PConv-Keras\data\logs\imagenet_phase2\weights.26-1.07.h5", train_bn=False)

But where can I download the trained model? I only want to run the test and do not want to perform training.

Padding part of K.conv2d()

Hi Mathias,
What is an advantage of padding the mask and the image outside of K.conv2d() function (like you do here)?
It seems to me that your original implementation (with padding done inside K.conv2d() and using the "same" padding scheme) should give the same result. The normalisation is based on the mask padded the same way and has pixels near the edges down-weighted properly. What am I missing?

how did you found out what U-net to use?

image

When I was reading the paper reference 11 is Isola, P., Zhu, J.Y., Zhou, T., Efros, A.A.: Image-to-image translation with conditional adversarial networks. arXiv preprint (2017).

Are you using a conditional adversarial network?

Mean normalization in partial convolution

Hi,
I have a question regarding your implementation of partial convolution layer. Your implementation employs mean normalization instead of sum normalization as it is in the original paper. Could you explain the intuition behind that change?

main() problem.

Found 0 images belonging to 0 classes.
Traceback (most recent call last):
File "/home//PConv-Keras-master/main.py", line 180, in
test_data = next(test_generator)
File "/home//PConv-Keras-master/main.py", line 122, in flow_from_directory
for _ in range(ori.shape[0])], axis=0
File "/home/zxf/anaconda3/envs/tensorflow/lib/python3.6/site-packages/numpy/core/shape_base.py", line 412, in stack
raise ValueError('need at least one array to stack')
ValueError: need at least one array to stack

excuse me . I follow the comand python main.py --name celeba --train ./data/celeba/train --validation ./data/celeba/val --test ./data/celeba/test --vgg_path './data/logs/pytorch_to_keras_vgg16.h5'
but there is no found files about mask and image, why?? i put masks dataset under ./data/masks/train/

Notebook 5 references "random_mask" which isn't available.

From Notebook 5, cell 2

if os.path.basename(os.getcwd()) != 'PConv-Keras':
    os.chdir('..')

from libs.pconv_model import PConvUnet
from libs.util import random_mask, ImageChunker

%load_ext autoreload
%autoreload 2

No such code is found.

I was able to get it to work by using MaskGenerator, I'm assuming you cleaned up the mask generation code then didn't update the notebook.

I'm working on PR

doubt in testing your code

@MathiasGruber I am trying to test your code on my own masks. I have made the necessary changes. My doubt is what color masks does the code expect. Should the masked region be red (like in your demo) or white like shown below??
add

Similarly should the mask be black-over-white or white-over-black??
mask

Rebuilding model during load gives error for multi GPU model

self.model, inputs_mask = self.build_pconv_unet(train_bn)

Rebuilding the model without the init options gives error when training on multi GPU.
Error traceback:

Traceback (most recent call last):
  File "train_pconvunet.py", line 270, in <module>
    model.load(args.checkpoint)
  File "/home/ubuntu/image-inpainting/libs/pconv_model.py", line 307, in load
    self.model.load_weights(filepath)
  File "/home/ubuntu/image-inpainting/env/lib/python3.6/site-packages/keras/engine/topology.py", line 2667, in load_weights
    f, self.layers, reshape=reshape)
  File "/home/ubuntu/image-inpainting/env/lib/python3.6/site-packages/keras/engine/topology.py", line 3365, in load_weights_from_hdf5_group
    str(len(filtered_layers)) + ' layers.')
ValueError: You are trying to load a weight file containing 1 layers into a model with 31 layers.

Commenting the rebuilding and compilation statements in the load function works however.

how to set up a page like fixmyphoto

Hi, i want to train my own dataset, and after that I try to set up a page to interactively put mask on image and get result. can you share your code of fixmyphoto page or would you please tell me how to do it? thanks.

Question about the PConv Layer

In the Paper, authors use the sum(M) means the sum of the conv slide window binary mask to make sure that do not use the hole pixels.
But i read your code , do you use the mean value of the whole mask? If so , you use the hole pixels' values

mlmodel file

Hello. How can I convert your script to mlmodel file to use in the ios app?(swift)
I try to use this tutorial but I need your help please.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.