Code Monkey home page Code Monkey logo

kaggle_dstl_submission's Introduction

THIS REPO IS UNMAINTEINED AND MOSTLY REMAINS AS HISTORIC ARTIFACT.

Please, take a look at versions of software we used to generate our result -- it is ancient now. Do not expect this code to run on Keras above version 1.2.2 and so on. It will not run out of the box, we wouldn't answer any issues about it, sorry. If you manage to run this code on newer versions -- please, feel free to open pull request, we will merge it for the public good.

Take a look at https://github.com/ternaus/TernausNet if you need more up-to-date segmentation solution.

Winning Model Documentation

Name: Vladimir Iglovikov

LinkedIn: https://www.linkedin.com/in/iglovikov/

Location: San-Francisco, United States

Name: Sergey Mushinskiy

LinkedIn: https://www.linkedin.com/in/sergeymushinskiy/

Location: Angarsk, Russia

Competition: Dstl Satellite Imagery Feature Detection

Blog post: http://blog.kaggle.com/2017/05/09/dstl-satellite-imagery-competition-3rd-place-winners-interview-vladimir-sergey/

If you find this code useful for your publications, please consider citing

@article{DBLP:journals/corr/IglovikovMO17,
  author    = {Vladimir Iglovikov and
               Sergey Mushinskiy and
               Vladimir Osin},
  title     = {Satellite Imagery Feature Detection using Deep Convolutional Neural
               Network: A Kaggle Competition},  
  volume    = {abs/1706.06169},
  year      = {2017},  
  archivePrefix = {arXiv},
  eprint    = {1706.06169},     
}

Prerequisites

To train final models you will need the following:

  • OS: Ubuntu 16.04 (although code was successfully ran on Windows 10 too)
  • Required hardware:
    • Any decent modern computer with x86-64 CPU,
    • Fair amount of RAM (we had about 32Gb and 128Gb in our boxes, however, not all memory was used)
    • Powerful GPU: we used Nvidia Titan X (Pascal) with 12Gb of RAM and Nvidia GeForce GTX 1080 with 8Gb of RAM.

Main software for training neural networks:

  • Python 2.7 (preferable and fully tested) or Python 3.5
  • Keras 1.2.2
  • Theano 0.9.0rc1

Utility packages for geometry and image manipulation and other helper functions:

  • h5py
  • matplotlib
  • numba
  • numpy
  • pandas
  • rasterio
  • Shapely
  • scikit_image
  • tifffile
  • OpenCV
  • tqdm
  1. Install required OS and Python
  2. Install packages with pip install -r requirements.txt
  3. Create following directory structure:
  • Data structure:
data / theree_band / *
     / sixteen_band / *
    grid_sizes.csv
    train_wkt_v4.csv
  • Source code
src / *.py

Prepare data for training:

  1. Run python get_3_band_shapes.py
  2. Run cache_train.py

Train models

Each class in our solution has separate neural network, so it requires running of several distinct models one by one (or in parallel if there are enough computing resources)

  1. Run python unet_buidings.py
  2. Run python unet_structures.py
  3. Run python unet_road.py
  4. Run python unet_track.py
  5. Run python unet_trees.py
  6. Run python unet_crops.py

For water predictions we used different method and it can be created by running:

  1. Run python fast_water.py
  2. Run python slow_water.py

After training finishes (it may require quite a long time depending on hardware used, in our case it was about 7 hours for each stage (50 epochs)) trained weights and model architectures are saved in cache directory and can be used by prediction scripts (see the next section).

Create predictions

To create predictions run every make_prediction_cropped_*.py file in src dir. It could take considerable amount of time to generate all predictions as there are a lot of data in test and we use separate models for each class and use test time augmentation and cropping for the best model performance. On Titan X GPU each class took about 5 hours to get predictions.

  1. Run python make_prediction_cropped_buildings.py
  2. Run python make_prediction_cropped_structures.py
  3. Run python make_prediction_cropped_track.py
  4. Run python make_prediction_cropped_road.py
  5. Run python make_prediction_cropped_trees.py
  6. Run python make_prediction_cropped_crops.py

When all predictions are done they should be merged in a single file for submit:

  • Run python merge_predictions.py
  1. Run python merge_predictions.py The previous step will create file joined.csv that just merges predictions per class into the unified format.
  • Last step in the pipeline is to
  1. Run python post_processing.py joined.csv

that will perform some cleaning of the overlapping classes (remove predictions of the slow water from fast water, all other predictions from buildings, etc)

  • Done!

Remarks

Please, keep in mind that this isn't a production ready code but a very specific solution for the particular competition created in short time frame and with a lot of other constrains (limited training data, scarce computing resources and a small number of attents to check for improvements).

So, there are a lot of hardcoded magic numbers and strings and there may be some inconsistensies and differences between different models. Sometimes, it was indentended to get more accurate predictions and there wasn't enough resources to check if changes improve score for other classes after they were introduced for some of them. Sometimes, it just slipped from our attention.

Also, inherent stochasticity of neural networks training on many different levels (random initialization of weights, random cropping of patches into minibatch and so on) makes it impossible to reproduce exact submission from scratch. We went extra mile and reimplemented solution and training procedure from scratch as much as possible in the last two weeks after competition final. We've got up to 20% extra performance for some classes with abundant training data like buildings, tracks and so on. However, some classes proven more difficult to reliably reproduce because of lack of training data and small amount of time. Such classes show high variance of results between epochs. For competition we used our best performing combinations of epoch/model for those classes, which may not be exactly the same as trained for fixed number of epochs (as in this particular code). However, we believe that our model is equally capable to segment any classes, given enough data and/or clear definitions what exactly consists of each class (it wasn't clear how segmentation was performed in the first place for some classes, like road/tracks).

kaggle_dstl_submission's People

Contributors

cepera-ang avatar ternaus 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

kaggle_dstl_submission's Issues

ValueError: "concat" mode can only merge layers with matching output shapes except for the concat axis. Layer shapes: [(None, 1024, 14, 7), (None, 256, 14, 14)]

I ran the make_prediction_cropped_buildings.py ,after below classes

  1. python get_3_band_shapes.py
  2. cache_train.py

got the ValueError.

python make_prediction_cropped_buildings.py
Using TensorFlow backend.
Traceback (most recent call last):
File "make_prediction_cropped_buildings.py", line 26, in
model = read_model()
File "make_prediction_cropped_buildings.py", line 21, in read_model
model = model_from_json(open(os.path.join('../src/cache', json_name)).read())
File "/home/darshan/anaconda2/lib/python2.7/site-packages/keras/models.py", line 214, in model_from_json
return layer_from_config(config, custom_objects=custom_objects)
File "/home/darshan/anaconda2/lib/python2.7/site-packages/keras/utils/layer_utils.py", line 44, in layer_from_config
custom_objects=custom_objects)
File "/home/darshan/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 2596, in from_config
process_layer(layer_data)
File "/home/darshan/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 2592, in process_layer
layer(input_tensors)
File "/home/darshan/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1469, in call
node_indices, tensor_indices)
File "/home/darshan/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1379, in _arguments_validation
'Layer shapes: %s' % (input_shapes))
ValueError: "concat" mode can only merge layers with matching output shapes except for the concat axis. Layer shapes: [(None, 1024, 14, 7), (None, 256, 14, 14)]

Can any one help me on this?

Thanks

Thanks so much

Thanks so much for your code, I have learned a lot from it.
I have a question just bugging me,
how did you choose the number of epochs for each unet? was it just through a train_test validation split or through cross validation?
Also, generally, how do you choose the number of epochs for large computer vision data sets and do you always perform cross validation?

Thanks again.

Training at once for all classes

I have a problem statement in which there are total 16 classes. I trained with Segnet with all classes at once. Results are not that good.
And I saw your solution.
I wanted to know which one is more suitable:

  1. training for each of the classes separately
  2. training for all classes at once.

Did you tried training of all classes at once. I just wanted to know you experience and suggestions regarding this.

output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: None

When I try to run unet_buildings.py, I encounter this issue. I changed the channel from 16+3 to only RGB channels but I don't think that will cause the issue.

Here are the traceback:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/root/anaconda3/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/root/anaconda3/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/root/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 429, in data_generator_task
generator_output = next(self._generator)
TypeError: 'threadsafe_iter' object is not an iterator
Epoch 1/50

Traceback (most recent call last):
File "unet_buildings.py", line 278, in
nb_worker=8
File "/root/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1532, in fit_generator
str(generator_output))
ValueError: output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: None

make prediction on buildings is failing

Dear all,

when I try to perform a prediction on the buildings (after the training) I get the following issue:

Using TensorFlow backend.
2017-12-04 14:24:44.615883: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2
0%| | 0/425 [00:00<?, ?it/s]Traceback (most recent call last):
File "/home/apache/kaggle_dstl_submission/src/make_prediction_cropped_buildings.py", line 95, in
result += [(image_id, mask_channel + 1, mask2poly(new_mask, threashold, x_scaler, y_scaler))]
File "/home/apache/kaggle_dstl_submission/src/extra_functions.py", line 125, in mask2polygons_layer
contours, hierarchy = cv2.findContours(((mask == 1) * 255).astype(np.uint8), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS)
ValueError: too many values to unpack

Can you please help?

Bests
G.

License == ?

Hi, I would like to know under which license is your work available. GitHub's default license is "all rights reserved" for public repositories, such as this one.

I have read the following, about winners' obligations of publishing the solution: https://www.kaggle.com/c/dstl-satellite-imagery-feature-detection/rules
I am not exactly sure of what type of competition this is, so I am not sure under which license you have published your solution according to the rules. I would like to know it. Adding a license to your repository is simple:
https://help.github.com/articles/adding-a-license-to-a-repository/
Just be sure that in the template you pick to add your names.

By the way, the following piece of code is licensed Apache 2.0: https://www.kaggle.com/ceperaang/lb-0-42-ultimate-full-solution-run-on-your-hw/code/code
And it also use other Apache 2.0 Licensed content. But the license for that kernel may not be the same as for this GitHub repository.

Many thanks!

Running code only with RGB images

Dear all,

I was able to run the code with the Kaggle image set, I would like to perform some predictions on sattelite photos of mine, but they are only in RGB (I don't have any Panachormatic and others wavelength images), is this possible?

Thanks
G.

why loss go up?

when i use jaccard as loss function, the training loss dose not go down but go up. what is wrong with me?

model issue

Hi,
In your code, the unet model in 'unet_structure.py', 'unet_building.py', 'unet_road.py' seem to be the same? I have not found any difference in the model definition. What is the point of using different model for different objects?
Best regards!

ValueError: "concat" mode can only merge layers with matching output shapes except for the concat axis. Layer shapes: [(None, 2, 14, 512), (None, 2, 14, 256)]

I run you code "unet_buildings.py" to train model. then I got an error as following:

[2017-11-16 09:49:53.996358] Creating and compiling model...
Traceback (most recent call last):
File "unet_buildings.py", line 254, in
model = get_unet0()
File "unet_buildings.py", line 104, in get_unet0
up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 1680, in merge
name=name)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 1299, in init
node_indices, tensor_indices)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 1371, in _arguments_validation
'Layer shapes: %s' % (input_shapes))
ValueError: "concat" mode can only merge layers with matching output shapes except for the concat axis. Layer shapes: [(None, 2, 14, 512), (None, 2, 14, 256)]

I use keras+Theano+GPU.
Any idea why it is crashing?

Many Thanks!

Reproducing Results Problem

Hi.
Great work and thanks for code.
I was trying to repeat your results on building class (which look impressive) and I just ran your make preduction building file with the model saved in the cache without making any changes in the code. I evaluated the output csv and noticed that the reported 70%ish performabce drops to roughly 35%. Have you changed the building prediction file that the results do not match?

Thanks.

Object detection question

Hi!
Sorry for raising an issue, I found it as a way of reaching out to you.
I am very impressed by your solutions for many image segmentation competitions, where participants are provided with image and ground-truth labels.
However I wanted to ask about your approach in cases when ground truth is only point, not mask (for example https://www.datasciencechallenge.org/challenges/1/safe-passage competition).
Could you reveal your appropach to such a problem? How to prepare masks in such a case, what kind of networks to use?

Kind regards,
Tomasz

RGB implementation

I want to implement your pre-trained model of DSTL competition only on RGB images. I don't have data set of other band images for my satellite imagery application. Is it possible to test only RGB images using your pre-trained model?

Running visualize error

After running visualize I get the following error: [Errno 2] No such file or directory: 'predictions/Objects--6080_4_4.png'.
I've followed all the steps. Is there something wrong I didn't pay attention to?
Thank you so much.

Dimension of the traning set images

Dear all,
I see that in the code the img_rows and img_cols is set to 112, has that a special meaning or is it the best value that you found during the training?

Thanks
G.

Using real coordinates

Dear all,

I'm trying to use the NN with satellite pictures with real coordinates and different projection, is there anything I should change or adapt? Right now I'm getting a very big loss:
8832/25600 [=========>....................] - ETA: 15210s - loss: 39.2488 - binary_crossentropy: 0.2799 - jaccard_coef_int: 0.5290

Does it make any difference if the images are not on the 0,0 with the top left corner?

Bests
G.

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.