An Implementation of Fully Convolutional Networks in Tensorflow.

Overview

Update

An example on how to integrate this code into your own semantic segmentation pipeline can be found in my KittiSeg project repository.

tensorflow-fcn

This is a one file Tensorflow implementation of Fully Convolutional Networks in Tensorflow. The code can easily be integrated in your semantic segmentation pipeline. The network can be applied directly or finetuned to perform semantic segmentation using tensorflow training code.

Deconvolution Layers are initialized as bilinear upsampling. Conv and FCN layer weights using VGG weights. Numpy load is used to read VGG weights. No Caffe or Caffe-Tensorflow is required to run this. The .npy file for [VGG16] to be downloaded before using this needwork. You can find the file here: ftp://mi.eng.cam.ac.uk/pub/mttt2/models/vgg16.npy

No Pascal VOC finetuning was applied to the weights. The model is meant to be finetuned on your own data. The model can be applied to an image directly (see test_fcn32_vgg.py) but the result will be rather coarse.

Requirements

In addition to tensorflow the following packages are required:

numpy scipy pillow matplotlib

Those packages can be installed by running pip install -r requirements.txt or pip install numpy scipy pillow matplotlib.

Tensorflow 1.0rc

This code requires Tensorflow Version >= 1.0rc to run. If you want to use older Version you can try using commit bf9400c6303826e1c25bf09a3b032e51cef57e3b. This Commit has been tested using the pip version of 0.12, 0.11 and 0.10.

Tensorflow 1.0 comes with a large number of breaking api changes. If you are currently running an older tensorflow version, I would suggest creating a new virtualenv and install 1.0rc using:

export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.0.0rc0-cp27-none-linux_x86_64.whl
pip install --upgrade $TF_BINARY_URL

Above commands will install the linux version with gpu support. For other versions follow the instructions here.

Usage

python test_fcn32_vgg.py to test the implementation.

Use this to build the VGG object for finetuning:

vgg = vgg16.Vgg16()
vgg.build(images, train=True, num_classes=num_classes, random_init_fc8=True)

The images is a tensor with shape [None, h, w, 3]. Where h and w can have arbitrary size.

Trick: the tensor can be a placeholder, a variable or even a constant.

Be aware, that num_classes influences the way score_fr (the original fc8 layer) is initialized. For finetuning I recommend using the option random_init_fc8=True.

Training

Example code for training can be found in the KittiSeg project repository.

Finetuning and training

For training build the graph using vgg.build(images, train=True, num_classes=num_classes) were images is q queue yielding image batches. Use a softmax_cross_entropy loss function on top of the output of vgg.up. An Implementation of the loss function can be found in loss.py.

To train the graph you need an input producer and a training script. Have a look at TensorVision to see how to build those.

I had success finetuning the network using Adam Optimizer with a learning rate of 1e-6.

Content

Currently the following Models are provided:

  • FCN32
  • FCN16
  • FCN8

Remark

The deconv layer of tensorflow allows to provide a shape. The crop layer of the original implementation is therefore not needed.

I have slightly altered the naming of the upscore layer.

Field of View

The receptive field (also known as or field of view) of the provided model is:

( ( ( ( ( 7 ) * 2 + 6 ) * 2 + 6 ) * 2 + 6 ) * 2 + 4 ) * 2 + 4 = 404

Predecessors

Weights were generated using Caffe to Tensorflow. The VGG implementation is based on tensorflow-vgg16 and numpy loading is based on tensorflow-vgg. You do not need any of the above cited code to run the model, not do you need caffe.

Install

Installing matplotlib from pip requires the following packages to be installed libpng-dev, libjpeg8-dev, libfreetype6-dev and pkg-config. On Debian, Linux Mint and Ubuntu Systems type:

sudo apt-get install libpng-dev libjpeg8-dev libfreetype6-dev pkg-config
pip install -r requirements.txt

TODO

  • Provide finetuned FCN weights.
  • Provide general training code
Comments
  • Out of Memory if the batch size is larger than 5

    Out of Memory if the batch size is larger than 5

    I use GTX 1080 (8G). The size of images in train datasets are 640*480. When the batch size is larger than 5, GPU will out of memory. The weight is 515MB, so is there something wrong? Second, the training is very slow. If batch size is 2, five seconds are needed. Do you know the reason, thanks!@MarvinTeichmann

    opened by JackieLeeTHU11 6
  • Contribute to tflearn

    Contribute to tflearn

    @MarvinTeichmann thank you for this implementation of FCN in tensorflow. This is exactly what I was looking for. I'd like to humbly suggest you port your work into tflearn. I think that your implementation of _upscore_layer as well as the loss function you've defined would be very useful in tflearn.

    opened by FreakTheMighty 6
  • Error when run test_fcn32_vgg.py

    Error when run test_fcn32_vgg.py

    Hello Marvin,

    I have some trouble when I run your codes.

    When I run the test_fcn32_vgg.py, it reminds me that there is no enough memory to allocate the tensor. (with gpu) However, I can run the test_fcn16_vgg.py(with gpu), it works well. And then I tried running with only cpu, both test_fcn32_vgg.py and test_fcn16_vgg.py works well.

    I read the codes and it seems that test_fcn16_vgg.py may need more memory?

    Is it just for that my memory of GPu is not enough (it is about 2GB) or something else?

    I run the test code, but the result is not so good. fcn32_upsampled

    And you can see the part of the dog is missing, which is not like your result on github.

    Thanks for your time.

    Sincerely, Shuhong

    opened by Monsterhoho 4
  • running error from

    running error from "python test_fcn32_vgg.py"

    Hi, all,

    I got the following error when running "python test_fcn32_vgg.py"

    InternalError (see above for traceback): Dst tensor is not initialized.
    	 [[Node: fc6/weights/Initializer/Const = Const[_class=["loc:@fc6/weights"], dtype=DT_FLOAT, value=Tensor<type: float shape: [7,7,512,4096] values: [[[1.9745843e-05 0.00035308721 -0.0018327669]]]...>, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
    

    My version of tf is r0.12.1 and I checkout the package (bf9400c6303826e1c25bf09a3b032e51cef57e3b) already. Could someone suggest me how to fix it?

    THX!

    opened by amiltonwong 4
  • AttributeError: type object 'GraphKeys' has no attribute 'REGULARIZATION_LOSSES'

    AttributeError: type object 'GraphKeys' has no attribute 'REGULARIZATION_LOSSES'

    Hi! I got this error.

    npy file loaded Layer name: conv1_1 Layer shape: (3, 3, 3, 64) Traceback (most recent call last): File "test_fcn32_vgg.py", line 24, in vgg_fcn.build(batch_images, debug=True) File "/home/sheldon/tensorflow-fcn-master/fcn32_vgg.py", line 74, in build self.conv1_1 = self._conv_layer(bgr, "conv1_1") File "/home/sheldon/tensorflow-fcn-master/fcn32_vgg.py", line 135, in _conv_layer filt = self.get_conv_filter(name) File "/home/sheldon/tensorflow-fcn-master/fcn32_vgg.py", line 259, in get_conv_filter tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, AttributeError: type object 'GraphKeys' has no attribute 'REGULARIZATION_LOSSES'

    python 2.7 tensorflow1.2 Could you give me some suggestions?

    opened by FLSD-LEO 3
  • Can we store the fine-tuned weight using tf.train.Saver()?

    Can we store the fine-tuned weight using tf.train.Saver()?

    Hi, I am fine-tuning your FCN toward my own dataset. But after tuning, I was doing stuff like 'conv1_1/filter:0','conv1_1/biases:0'; sess.run(filters) to extract features and save them into npy file. But since we have lots of filters here, listing all of them would be messy.

    So I was wondering if we can do it with the tf.train.Saver, which allows us to store the trained Variables fast and clean. But later I notice that there is no "Variable" in the fcn8s_vgg file. And I am thinking that by using your original fcn8s_vgg.py file, we can't use the tf.train.Saver(), right?

    I am new to tensorflow, please let me know your comments, thanks.

    opened by robot010 3
  • Error in loss function

    Error in loss function

       epsilon = tf.constant(value=1e-4)     
       logits = logits + epsilon
       softmax = tf.nn.softmax(logits)
    

    It should be epsilon = tf.constant(value=1e-4)
    softmax = tf.nn.softmax(logits) + epsilon

    opened by ymzhang1919 3
  • training dataset

    training dataset

    After reading the readme file, it's still not clear to me what data set was used for training? I understand some filters are initialized from vgg16, then training is done on what dataset?

    By "No Pascal VOC finetuning was applied to the weights.", I guess the training set is not Pascal VOC?

    Thanks,

    opened by weiliu620 3
  • Problems with gray images

    Problems with gray images

    Hi Mr.Marvin Teichmann I found this in the usage that: The images is a tensor with shape [None, h, w, 3]. Where h and w can have arbitrary size.

    My training data is gray images with no rgb channels. Can I still use this code and vgg to train them?

    opened by jiao0805 2
  • regarding using this model for my own data set with only two classes

    regarding using this model for my own data set with only two classes

    Hi Marvin,

    Thank you for sharing the code. May I ask you for some advice?

    I am trying to adopting your code for one of my data set, which has about 300 images with 600*800 sizes. The masked one has about two classes, class 1 takes about 20% of the whole image. The image itself is kind of far away from the data set used to pre-train the VGG model. For instance, the bio-medical data.

    There are several questions,

    1. Can I still use the pre-trained VGG weights?
    2. Which part of the code need I change to incorporate the two-classes case?
    3. Since the image size is pretty big and the number of images is limited, I have been planning to conduct patch-wise training, i.e., creating sample patches from the original images. Should I heavily sample the areas corresponding to class 1? During the training process, do I have to put all the patches corresponding to a single image into a single batch? Or I can just use batch size=1 in the training process?

    Wenouyang

    opened by wenouyang 2
  • target_size=None  can not work.

    target_size=None can not work.

    I found code about the input shape.

        if target_size:
            input_shape = target_size + (3,)
        else:
            input_shape = (None, None, 3)
    

    When I set target_size=None, error encountered as below:

    Traceback (most recent call last):
      File "train.py", line 132, in <module>
        data_dir, label_dir, target_size=target_size, resume_training=True)
      File "train.py", line 102, in train
        batch_size=batch_size, shuffle=True
      File "/data/kuixu/exper/fcn/Keras-FCN/utils/SegDataGenerator.py", line 186, in flow_from_directory
        save_to_dir=save_to_dir, save_prefix=save_prefix, save_format=save_format)
      File "/data/kuixu/exper/fcn/Keras-FCN/utils/SegDataGenerator.py", line 26, in __init__
        self.target_size = tuple(target_size)
    TypeError: 'NoneType' object is not iterable
    
    

    please help

    opened by kuixu 2
  • The weight decay (the regularization loss) is not applied for the fc_layer and upscore_layer

    The weight decay (the regularization loss) is not applied for the fc_layer and upscore_layer

    Hello! I found that in fcn8s_vgg.py. The weight decay loss is not added into tf.GraphKeys.REGULARIZATION_LOSSES, since you specify another collection name.

    I would like to know if this will affect the final results?

    opened by SpinachR 0
  • convert the ckpt model to movidius graph

    convert the ckpt model to movidius graph

    when I convert the ckpt model to movidius graph, I meet the problem: InvalidArgumentError (see above for traceback): Number of ways to split should evenly divide the split dimension, but got split_dim 3 (size = 224) and num_split 3 [[Node: Validation/Validation/Processing/split = Split[T=DT_FLOAT, num_split=3, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Validation/Validation/Processing/split/split_dim, Validation/ExpandDims)]]」

    then I find the code in tensorflow split_op.cc OP_REQUIRES(context, input_shape.dim_size(split_dim) % num_split == 0, errors::InvalidArgument( "Number of ways to split should evenly divide the split " "dimension, but got split_dim ", split_dim, " (size = ", input_shape.dim_size(split_dim), ") ", "and num_split ", num_split));

    I read code in KittiSeg/submodules/tensorflow-fcn/fcn8_vgg.py , the following code execute "tf.split()" function.

    
             red, green, blue = tf.split(rgb, 3, 3)
    
            # assert red.get_shape().as_list()[1:] == [224, 224, 1]
            # assert green.get_shape().as_list()[1:] == [224, 224, 1]
            # assert blue.get_shape().as_list()[1:] == [224, 224, 1]
            bgr = tf.concat([
                blue - VGG_MEAN[0],
                green - VGG_MEAN[1],
                red - VGG_MEAN[2],
            ], 3)
    

    this code means the image channels which divided by 3, but the log shows 224 divided 3 , so 224%3 != 0.

    opened by zhanghanbin3159 1
  • What does the

    What does the "use_dilated" mean in "build" function?

    I can't understand what codes below want to do, does anybody can explain? Thanks so much !!!

    if use_dilated:
        pad = [[0, 0], [0, 0]]
        self.pool4 = tf.nn.max_pool(self.conv4_3, ksize=[1, 2, 2, 1],
                                    strides=[1, 1, 1, 1],
                                    padding='SAME', name='pool4')
        self.pool4 = tf.space_to_batch(self.pool4,
                                       paddings=pad, block_size=2)
    else:
        self.pool4 = self._max_pool(self.conv4_3, 'pool4', debug)
    
    opened by VincentFEI 1
  • is the vgg16.npy random weights or trained weights....?

    is the vgg16.npy random weights or trained weights....?

    After loading the weights still the output is random. What should i change in the code after giving correct path of .npy file ?? Please tell me how to predict loading correct weights my output image :- test_lane

    opened by ChinmayKalyankar 0
  • The problem about  fcn8_vgg.py

    The problem about fcn8_vgg.py

    Hi Marvin,thanks for your code! I have two problems. Can you help me ? First,I want to know why you convert RGB to BGR in fcn8_vgg ? second,what's the meaning of the code as follows ( in line 123) : if use_dilated: self.pool5 = tf.batch_to_space(self.pool5, crops=pad, block_size=2) self.pool5 = tf.batch_to_space(self.pool5, crops=pad, block_size=2) self.fc7 = tf.batch_to_space(self.fc7, crops=pad, block_size=2) self.fc7 = tf.batch_to_space(self.fc7, crops=pad, block_size=2)

    opened by SmmileIU 0
Owner
Marvin Teichmann
Germany Phd student. Working on Deep Learning and Computer Vision projects.
Marvin Teichmann
A PyTorch implementation for V-Net: Fully Convolutional Neural Networks for Volumetric Medical Image Segmentation

A PyTorch implementation of V-Net Vnet is a PyTorch implementation of the paper V-Net: Fully Convolutional Neural Networks for Volumetric Medical Imag

Matthew Macy 606 Dec 21, 2022
PyTorch Implementation of Fully Convolutional Networks. (Training code to reproduce the original result is available.)

pytorch-fcn PyTorch implementation of Fully Convolutional Networks. Requirements pytorch >= 0.2.0 torchvision >= 0.1.8 fcn >= 6.1.5 Pillow scipy tqdm

Kentaro Wada 1.6k Jan 7, 2023
Another pytorch implementation of FCN (Fully Convolutional Networks)

FCN-pytorch-easiest Trying to be the easiest FCN pytorch implementation and just in a get and use fashion Here I use a handbag semantic segmentation f

Y. Dong 158 Dec 21, 2022
Unofficial TensorFlow implementation of Protein Interface Prediction using Graph Convolutional Networks.

[TensorFlow] Protein Interface Prediction using Graph Convolutional Networks Unofficial TensorFlow implementation of Protein Interface Prediction usin

YeongHyeon Park 9 Oct 25, 2022
The official PyTorch implementation of the paper: *Xili Dai, Xiaojun Yuan, Haigang Gong, Yi Ma. "Fully Convolutional Line Parsing." *.

F-Clip — Fully Convolutional Line Parsing This repository contains the official PyTorch implementation of the paper: *Xili Dai, Xiaojun Yuan, Haigang

Xili Dai 115 Dec 28, 2022
End-to-End Object Detection with Fully Convolutional Network

This project provides an implementation for "End-to-End Object Detection with Fully Convolutional Network" on PyTorch.

null 472 Dec 22, 2022
Dewarping Document Image By Displacement Flow Estimation with Fully Convolutional Network.

Dewarping Document Image By Displacement Flow Estimation with Fully Convolutional Network

null 111 Dec 27, 2022
Dewarping Document Image By Displacement Flow Estimation with Fully Convolutional Network

Dewarping Document Image By Displacement Flow Estimation with Fully Convolutional Network

null 39 Aug 2, 2021
Speech Separation Using an Asynchronous Fully Recurrent Convolutional Neural Network

Speech Separation Using an Asynchronous Fully Recurrent Convolutional Neural Network This repository is the official implementation of Speech Separati

Kai Li (李凯) 116 Nov 9, 2022
CoSMA: Convolutional Semi-Regular Mesh Autoencoder. From Paper "Mesh Convolutional Autoencoder for Semi-Regular Meshes of Different Sizes"

Mesh Convolutional Autoencoder for Semi-Regular Meshes of Different Sizes Implementation of CoSMA: Convolutional Semi-Regular Mesh Autoencoder arXiv p

Fraunhofer SCAI 10 Oct 11, 2022
This project is a loose implementation of paper "Algorithmic Financial Trading with Deep Convolutional Neural Networks: Time Series to Image Conversion Approach"

Stock Market Buy/Sell/Hold prediction Using convolutional Neural Network This repo is an attempt to implement the research paper titled "Algorithmic F

Asutosh Nayak 136 Dec 28, 2022
Pytorch implementation of AngularGrad: A New Optimization Technique for Angular Convergence of Convolutional Neural Networks

AngularGrad Optimizer This repository contains the oficial implementation for AngularGrad: A New Optimization Technique for Angular Convergence of Con

mario 124 Sep 16, 2022
PyTorch implementation of "ContextNet: Improving Convolutional Neural Networks for Automatic Speech Recognition with Global Context" (INTERSPEECH 2020)

ContextNet ContextNet has CNN-RNN-transducer architecture and features a fully convolutional encoder that incorporates global context information into

Sangchun Ha 24 Nov 24, 2022
PyTorch implementation of Graph Convolutional Networks in Feature Space for Image Deblurring and Super-resolution, IJCNN 2021.

GCResNet PyTorch implementation of Graph Convolutional Networks in Feature Space for Image Deblurring and Super-resolution, IJCNN 2021. The code will

null 11 May 19, 2022
A PyTorch implementation of " EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks."

EfficientNet A PyTorch implementation of EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks. [arxiv] [Official TF Repo] Implemen

AhnDW 298 Dec 10, 2022
Official PyTorch Implementation of Convolutional Hough Matching Networks, CVPR 2021 (oral)

Convolutional Hough Matching Networks This is the implementation of the paper "Convolutional Hough Matching Network" by J. Min and M. Cho. Implemented

Juhong Min 70 Nov 22, 2022
A PyTorch implementation of "Cluster-GCN: An Efficient Algorithm for Training Deep and Large Graph Convolutional Networks" (KDD 2019).

ClusterGCN ⠀⠀ A PyTorch implementation of "Cluster-GCN: An Efficient Algorithm for Training Deep and Large Graph Convolutional Networks" (KDD 2019). A

Benedek Rozemberczki 697 Dec 27, 2022
PyTorch implementation for Convolutional Networks with Adaptive Inference Graphs

Convolutional Networks with Adaptive Inference Graphs (ConvNet-AIG) This repository contains a PyTorch implementation of the paper Convolutional Netwo

Andreas Veit 176 Dec 7, 2022