We can easily profile functions in Python using line_profiler package. This package profiling functions simply using decorator around these functions.

First Install package:

pip install line_profiler

And add decorator @profile around function

def fact(n):
if n == 0: return 1
elif n == 1: return 1

Then use profiler. First create dump file for out test.py

kernprof --line-by-line test.py

This command will create file test.py.lprof

Now run profiler

python -m line_profiler test.py.lprof


Timer unit: 1e-06 sTotal time: 1e-05 s
File: test.py
Function: fact at line 1
Line # Hits Time Per Hit % Time Line Contents
1 @profile
2 def fact(n):
3 3 3.0 1.0 30.0 if n == 0: return 1
4 3 2.0 0.7 20.0 elif n == 1: return 1
5 2 5.0 2.5 50.0 …

Image for post
Image for post

Sometimes when run PyTorch model with GPU on Kaggle we get error “RuntimeError: CUDA out of memory. Tried to allocate …”

Clear memory with command:


Check CUDA memory

!pip install GPUtil
from GPUtil import showUtilization as gpu_usage


| ID | GPU | MEM |
| 0 | 0% | 0% |

We had a task to find best matching image to the given query image.

First we tried classic image matching with SIFT descriptors and Flann based matcher in OpenCV. Results were completely wrong. Then Bag-of-Words …

Finally we have learnt about combination of AffNet and HardNet from these slides.

AffNet is a novel method for learning local affine-covariant regions with
the hard negative-constant loss which outperform s the state-of-the-art in Bag-of-Words image retrieval and wide baseline stereo.

Image for post
Image for post
AffNet training (from original paper)

HardNet is a novel compact learned feature descriptor showing state-of-art superior performance relating to both hand-crafted and learned descriptors on standard matching and retrieval benchmarks and it is fast to compute on a GPU (authors paper). …

Lets imagine we need to compile some C++ library and get error telling that newer version of cmake is required. For instance, I had cmake v2.8 but version larger than 3.1 was required.

Compile cmake from source

$ sudo su

And compile it

# apt-get update
# apt-get dist-upgrade
# cd /Downloads
/Downloads# wget https://cmake.org/files/v3.5/cmake-3.5.0-rc1.tar.gz
/Downloads# tar xzf cmake-3.5.0-rc1.tar.gz
/Downloads# cd cmake-3.5.0-rc1

Rename the existing installation directory to the new version where, “x.x” is the existing installation directory and “3.5” will be the new name. …

Image for post
Image for post

We had a task to detect anomalies (oil spill, excavation, materials storage etc) from UAV photos. There is no dataset of UAV photos containing such anomalies. In this post I will tell how to training YOLOv5 object detection model from scratch on own dataset.

Task definition

Oil company using drones for monitoring oil production areas. There some anomalies could take place like oil spill, excavation, storage of material,
work of special equipment. This process is done manually which is time consuming. Artificial intelligence can be used here to automatically detect such anomalies from UAV photos. …

Image for post
Image for post

In my recent post I have presented a guide on training YOLOv3 darknet model on own dataset. In this post I will explain how to train YOLOv3 darknet model from AlekseyAB on own dataset in Goolge Colab.

I will omit preparing training data as it is covered in my previous post.

I my previous post I told about labelMe tool for labeling training samples. You can use also labeImg img. Install labelImg following official guide. If you face problem similar to mine just run this command:

sudo python3 labelImg.py

To mount Google Drive in Google Colab use drive API

from google.colab import…

Here I will present my observations on style guidelines and best practices in C++.

const reference parameters for input values

Use const reference parameters for input values (for large objects like class instances) to avoid changing objects and unnecessary deep-copying

void addFile(const std::string& filename,
const std::set<std::string>& filePaths)
bool operator==(const Position &other) {}

const member functions

Use const specifier for getters in user defined classes

int x() const { return x_; }

This will guarantee that the method will not change the private class member because member functions marked const cannot change member variables or call non-const methods on them.

Default constructor for class

When you define constructor with parameters in class Animal compiler do not generate default constructor…

Image for post
Image for post

We had a task to detect garbage trucks on video however popular datasets like COCO don’t include classes for garbage truck. In this post I will show how to create own dataset for object detection with own classes, train YOLOv3 model on this dataset and test it on some images and videos.

Choosing CNN model

We have studied benchmarks and results of experimental comparison of different models for object detection. Here is a good comparison of SOTA models. SSD with MobileNet provides the best accuracy and speed tradeoff, but has problems with detecting small objects. …

Image for post
Image for post

There is two types of copying objects in Python: deep and shallow. Here I will show how to perform both of them using built-in module copy.

Import module copy in your code

import copy

Try shallow copy

a = [4]b = copy.copy(a)
>>> b
>>> id(a)
>>> id(b)

Let’s see how it works on deeply nested lists

>>> a = [[1], [2], [3]]
>>> b = copy.copy(a)
>>> a.append([6])
>>> a
[[1], [2], [3], [6]]
>>> b
[[1], [2], [3]]
>>> a[2][0] = 4
>>> a
[[1], [2], [4], [6]]
>>> b
[[1], [2], [4]]

Change of element a[2][0] leading to change in corresponding element b[2][0] as all nested lists in a are passed as reference on default. …

Besides curl we can use wget command to test our REST API.

Send GET query:


Send POST query:

wget --post-data 'url=https://habr.com/ru/post/307140/'

Send POST query with params saved in params.txt file

wget --post-file params.txt

This will save result in index.html. If you want to output result to terminal use command:

wget -O - -q --post-file params.txt

Privalov Vladimir

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store