This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer