Skip to content

Instantly share code, notes, and snippets.

View Diyago's full-sized avatar
🎯
Focusing

Insaf Ashrapov Diyago

🎯
Focusing
View GitHub Profile
@Diyago
Diyago / .py
Created May 25, 2018 22:19
generate features
#export PATH=~/anaconda3/bin:$PATH
from tqdm import tqdm
import tensorflow as tf
from keras.applications.resnet50 import ResNet50
from keras.layers import Flatten, Input
from keras.models import Model
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
import numpy as np
from keras import layers
def residual_block(y, nb_channels, _strides=(1, 1), _project_shortcut=False):
shortcut = y
# down-sampling is performed with a stride of 2
y = layers.Conv2D(nb_channels, kernel_size=(3, 3), strides=_strides, padding='same')(y)
y = layers.BatchNormalization()(y)
y = layers.LeakyReLU()(y)
@Diyago
Diyago / pandas reducing memory.py
Created September 8, 2018 10:56
Reducing memory usage by pandas dataframe
def reduce_mem_usage(data, verbose = True):
start_mem = data.memory_usage().sum() / 1024**2
if verbose:
print('Memory usage of dataframe: {:.2f} MB'.format(start_mem))
for col in data.columns:
col_type = data[col].dtype
if col_type != object:
c_min = data[col].min()
@Diyago
Diyago / fitting.py
Last active August 4, 2019 12:40
fit Prophet.py
def fit_predict_model(dataframe, interval_width = 0.99, changepoint_range = 0.8):
m = Prophet(daily_seasonality = False, yearly_seasonality = False, weekly_seasonality = False,
seasonality_mode = 'multiplicative',
interval_width = interval_width,
changepoint_range = changepoint_range)
m = m.fit(dataframe)
forecast = m.predict(dataframe)
forecast['fact'] = dataframe['y'].reset_index(drop = True)
return forecast
def detect_anomalies(forecast):
forecasted = forecast[['ds','trend', 'yhat', 'yhat_lower', 'yhat_upper', 'fact']].copy()
#forecast['fact'] = df['y']
forecasted['anomaly'] = 0
forecasted.loc[forecasted['fact'] > forecasted['yhat_upper'], 'anomaly'] = 1
forecasted.loc[forecasted['fact'] < forecasted['yhat_lower'], 'anomaly'] = -1
#anomaly importances
forecasted['importance'] = 0
def plot_anomalies(forecasted):
interval = alt.Chart(forecasted).mark_area(interpolate="basis", color = '#7FC97F').encode(
x=alt.X('ds:T', title ='date'),
y='yhat_upper',
y2='yhat_lower',
tooltip=['ds', 'fact', 'yhat_lower', 'yhat_upper']
).interactive().properties(
title='Anomaly Detection'
)
@Diyago
Diyago / Get toronto road dataset.py
Last active November 25, 2021 01:27
Download Massachusetts Roads Dataset from https://www.cs.toronto.edu/~vmnih/data/
# based on https://github.com/BBarbosa/tflearn-image-recognition-toolkit/blob/
# 4a0528dcfb206b1e45997f2fbc097aafacfa0fa0/scripts/html_link_parser.py
import re
import argparse
from PIL import Image
from io import BytesIO
from bs4 import BeautifulSoup
from skimage import io as skio
conda install -c conda-forge keras
pip install git+https://github.com/qubvel/efficientnet
pip install git+https://github.com/qubvel/classification_models.git
pip install git+https://github.com/qubvel/segmentation_models
pip install git+https://github.com/albu/albumentations
pip install tta-wrapper
def __init__(self, root_dir=r'../data/val_test', image_folder='img/', mask_folder='masks/',
batch_size=1, image_size=768, nb_y_features=1,
augmentation=None,
suffle=True):
self.image_filenames = listdir_fullpath(os.path.join(root_dir, image_folder))
self.mask_names = listdir_fullpath(os.path.join(root_dir, mask_folder))
self.batch_size = batch_size
self.augmentation = augmentation
self.image_size = image_size
self.nb_y_features = nb_y_features
def __getitem__(self, index):
data_index_min = int(index*self.batch_size)
data_index_max = int(min((index+1)*self.batch_size, len(self.image_filenames)))
indexes = self.image_filenames[data_index_min:data_index_max]
this_batch_size = len(indexes) # The last batch can be smaller than the others
X = np.empty((this_batch_size, self.image_size, self.image_size, 3), dtype=np.float32)
y = np.empty((this_batch_size, self.image_size, self.image_size, self.nb_y_features), dtype=np.uint8)