This file contains 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
# with context内での実行の実行時間を計測するスクリプト | |
import time | |
class ElapseTimeInWithContext(object): | |
def __init__(self, context): | |
self.context = context | |
def __enter__(self): | |
self.start = time.time() | |
return self | |
def __exit__(self, *args): |
This file contains 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 base64_decoder(img_base64: str) -> np.ndarray: | |
img_base64 = img_base64.split('base64,')[-1] | |
img_data = base64.b64decode(img_base64) | |
img_np = np.fromstring(img_data, np.uint8) | |
img = cv2.imdecode(img_np, cv2.IMREAD_COLOR) | |
return img |
This file contains 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
class TimeoutScheduleMiddleware(object): | |
TIMEOUT_SEC = 3600 | |
def __init__(self, crawler): | |
self.crawler = crawler | |
self.scheduler_per_spider = {} | |
@classmethod | |
def from_crawler(cls, crawler): | |
# This method is used by Scrapy to create your spiders. |
This file contains 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
# ref: https://github.com/keras-team/keras-applications/blob/master/keras_applications/xception.py | |
# ref: https://arxiv.org/pdf/2010.02178.pdf | |
def ExtendXception(include_top=True, | |
weights='imagenet', | |
input_tensor=None, | |
pooling=None, | |
classes=1000, | |
**kwargs): | |
img_input = input_tensor | |
This file contains 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
import tensorflow as tf | |
K = tf.keras.backend | |
def l2_constrained_loss(norm): | |
def _l2_constrained_loss(y_true, y_pred): | |
y_pred = tf.nn.softmax(y_pred * norm) | |
return tf.keras.losses.categorical_crossentropy(y_true, y_pred) | |
return _l2_constrained_loss |
This file contains 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
import tensorflow as tf | |
def octave_conv2d(high_x, low_x, channels, alpha_in=0.5, alpha_out=0.5, stride=1, kernel_size=(3, 3), weight_decay=1e-4): | |
conv_hh = tf.keras.layers.Conv2D(int(channels * (1 - alpha_out)), | |
kernel_size, stride, padding='same', use_bias=False, | |
kernel_initializer=tf.keras.initializers.he_normal(), | |
kernel_regularizer=tf.keras.regularizers.l2(weight_decay), | |
bias_regularizer=tf.keras.regularizers.l2(weight_decay) | |
)(high_x) | |
This file contains 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
K = tf.keras.backend | |
class GradCam(object): | |
def __init__(self, model, loss=tf.keras.losses.CategoricalCrossentropy()): | |
self._model = model | |
self._loss_model_truth = tf.keras.layers.Input((model.layers[-1].output.shape[-1],)) | |
self._loss_model = loss(self._loss_model_truth, model.layers[-1].output) | |
def run(self, X, target_class_vector, target_layer): | |
conv_output = model.get_layer(name=target_layer).output |
This file contains 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
# https://kamosawa.hatenablog.com/entry/20151015 | |
repdict=dict() | |
for tap in [(c +'\u309a' , chr(ord(c)+2)) for c in u'はひふへほハヒフヘホ']: | |
repdict.update({tap[0]:tap[1]}) | |
for tap in [(chr(ord(c)) +'\u3099' , chr(ord(c)+1)) for c in u'かきくけこさしすせそたちつてとはひふへほカキクケコサシスセソタチツテトハヒフヘホ']: | |
repdict.update({tap[0]:tap[1]}) | |
def normalize_encode(contents): | |
for key in repdict.keys(): |
This file contains 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
# https://arxiv.org/pdf/1703.01780.pdf | |
import tensorflow as tf | |
from tensorflow.keras.callbacks import Callback | |
class TeacherOutputGetter(object): | |
def __init__(self, semi_supervised_X): | |
self._X = semi_supervised_X | |
self._teacher = None |
This file contains 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 manifold_mixup_loss(alpha=1.): | |
def _manifold_mixup_loss(y_true, y_pred): | |
''' | |
y_true: (batch_size, onehot_label_size) | |
y_pred: (batch_size, output_unit_size) | |
''' | |
dist = tf.compat.v1.distributions.Beta(alpha, alpha) | |
beta = dist.sample() |
NewerOlder