def __init__(self, img_height=128, img_width=128, img_channels=3, embedding_size=64, return_spatial_softmax=False): super(spatial_softmax_cnn, self).__init__() self.img_height = img_height self.img_width = img_width self.img_channels = img_channels self.rescaling = Rescaling( 1. / 255, input_shape=(img_height, img_width, img_channels)) # put it here for portability self.conv1 = Conv2D(32, 8, strides=(4, 4), padding='same', activation='relu', name='c1') self.conv2 = Conv2D(64, 4, strides=(2, 2), padding='same', activation='relu', name='c2') self.conv3 = Conv2D(64, 3, strides=(1, 1), padding='same', activation='relu', name='c3') # In between these, do a spatial softmax self.flatten = Flatten() self.dense1 = Dense(512, activation='relu') self.dense2 = Dense(embedding_size) self.return_spatial_softmax = return_spatial_softmax
def create_model(args, num_classes=5): def create_regularizer(): if args.l1 == None: return None if args.l2 == None else regularizers.l2(args.l2) else: return regularizers.l1( argsl1) if args.l2 == None else regularizers.l1_l2(l1=args.l1, l2=args.l2) def purgeNones( layers ): # Used to remove layers that have been set to None (e.g. unused Dropout) return [layer for layer in layers if layer is not None] product = Sequential( purgeNones([ Rescaling(1. / 255), Conv2D(32, 3, activation='relu'), MaxPooling2D(), Conv2D(32, 3, activation='relu'), MaxPooling2D(), Conv2D(32, 3, activation='relu'), MaxPooling2D(), Flatten(), Dropout(args.dropout) if args.dropout else None, Dense(128, activation='relu', kernel_regularizer=create_regularizer()), Dropout(args.dropout) if args.dropout else None, Dense(num_classes) ])) product.compile(optimizer='adam', loss=SparseCategoricalCrossentropy(from_logits=True), metrics=[SparseCategoricalAccuracy()]) return product
def model_builder(hp): #build a CNN dense = keras.layers.Dense(units=16) # Let's say we expect our inputs to be RGB images of arbitrary size inputs = keras.Input(shape=(None, None, 3)) from tensorflow.keras import layers # Center-crop images to 101 x 200 to fit sample size x = CenterCrop(height=201, width=200)(inputs) # Rescale images to [0, 1] x = Rescaling(scale=1./255)(x) # Apply some convolution and pooling layers x = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='SAME', activation='relu')(x) x = layers.MaxPooling2D(pool_size=(3, 3))(x) x = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='SAME', activation='relu')(x) # Apply global average pooling to get flat feature vectors x = layers.GlobalAveragePooling2D()(x) # add a dense layer x = layers.Dense(16, activation='relu')(x) # Add a dense classifier on top num_classes = 3 outputs = layers.Dense(num_classes, activation='sigmoid')(x) model = keras.Model(inputs=inputs, outputs=outputs) model.summary() # Tune the learning rate for the optimizer # Choose an optimal value from 0.01, 0.001, or 0.0001 hp_learning_rate = hp.Choice('learning_rate', values = [1e-2, 1e-3, 1e-4]) #compile and keep metrics #model.compile(optimizer=keras.optimizers.Adam(learning_rate = hp_learning_rate), # loss=keras.losses.SparseCategoricalCrossentropy(from_logits = True), # metrics=[keras.metrics.SparseCategoricalAccuracy(name='acc')]) model.compile(optimizer=keras.optimizers.Adam(learning_rate = hp_learning_rate), loss=keras.losses.BinaryCrossentropy()(from_logits = True), metrics=['accuracy']) #model.fit(x_train, y_train, batch_size=32, epochs=10) return model
def __init__( self, image_size, patch_size, num_layers, num_classes, d_model, num_heads, mlp_dim, channels=3, dropout=0.1, ): super(VisionTransformer, self).__init__() num_patches = (image_size // patch_size) ** 2 self.patch_dim = channels * patch_size ** 2 self.patch_size = patch_size self.d_model = d_model self.num_layers = num_layers self.rescale = Rescaling(1.0 / 255) self.pos_emb = self.add_weight( "pos_emb", shape=(1, num_patches + 1, d_model) ) self.class_emb = self.add_weight("class_emb", shape=(1, 1, d_model)) self.patch_proj = Dense(d_model) self.enc_layers = [ TransformerBlock(d_model, num_heads, mlp_dim, dropout) for _ in range(num_layers) ] self.layerNorm = tf.keras.Sequential( [ LayerNormalization(epsilon=1e-6), ] )
def build_model(img_shape, num_classes) -> Model: base_model = MobileNetV2(include_top=False, weights="imagenet", input_shape=IMAGENET_SHAPE) num_layers = len(base_model.layers) print(f"Number of layers in the base model: {num_layers}") fine_tune_at = num_layers - 10 for layer in base_model.layers[:fine_tune_at]: layer.trainable = False input_img = Input(shape=img_shape) x = Rescaling(scale=2.0, offset=-1.0)(input_img) x = Resizing(height=IMAGENET_SIZE, width=IMAGENET_SIZE)(x) x = base_model(x) x = GlobalAveragePooling2D()(x) x = Dense(units=num_classes)(x) y_pred = Activation("softmax")(x) model = Model(inputs=[input_img], outputs=[y_pred]) model.summary() return model
# print(labels_batch.shape) # break train_ds = train_ds.cache().shuffle(256).prefetch(buffer_size=AUTOTUNE) val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE) augment_data = Sequential([ RandomFlip("horizontal", input_shape=(img_height, img_width, 3)), RandomContrast(0.5), #, input_shape=(img_height, img_width, 3)), RandomZoom(0.2), RandomRotation(0.3), ]) model = Sequential([ augment_data, Rescaling(1. / 255, input_shape=(img_height, img_width, 3)), layers.Conv2D(16, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Conv2D(32, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Conv2D(64, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Dropout(0.2), layers.Flatten(), layers.Dense(128, activation='relu'), layers.Dense(num_classes) ]) model.compile( optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
y = layers.add([shortcut, y]) y = layers.LeakyReLU()(y) return y # In[8]: #%% RESIDUAL NEURAL NETWORK inputs = keras.Input(shape=(256, 256, 3)) # Center-crop images to 150x150 # x = CenterCrop(height=150, width=150)(inputs) # Rescale images to [0, 1] x = Rescaling(scale=1.0 / 255)(inputs) x = layers.Conv2D(filters=8, kernel_size=(3, 3), kernel_regularizer=tf.keras.regularizers.l1_l2(), padding='same')(x) x = layers.LeakyReLU()(x) x = layers.MaxPooling2D(pool_size=(2, 2))(x) x = residual_block(x, 8) x = layers.MaxPooling2D(pool_size=(2, 2))(x) x = residual_block(x, 16, _project_shortcut=True) x = layers.MaxPooling2D(pool_size=(2, 2))(x) x = residual_block(x, 32, _project_shortcut=True)
def imagenet_standardization(x): # scale images to a range (-1, +1) x = Rescaling(scale=1 / 127.5, offset=-1, name='rescaling')(x) return x
""" **Example: rescaling & center-cropping images** Both the `Rescaling` layer and the `CenterCrop` layer are stateless, so it isn't necessary to call `adapt()` in this case. """ from tensorflow.keras.layers.experimental.preprocessing import CenterCrop from tensorflow.keras.layers.experimental.preprocessing import Rescaling # Example image data, with values in the [0, 255] range training_data = np.random.randint(0, 256, size=(64, 200, 200, 3)).astype("float32") cropper = CenterCrop(height=150, width=150) scaler = Rescaling(scale=1.0 / 255) output_data = scaler(cropper(training_data)) print("shape:", output_data.shape) print("min:", np.min(output_data)) print("max:", np.max(output_data)) """ ## Building models with the Keras Functional API A "layer" is a simple input-output transformation (such as the scaling & center-cropping transformations above). For instance, here's a linear projection layer that maps its inputs to a 16-dimensional feature space: ```python dense = keras.layers.Dense(units=16) ```
def _build_preprocessing() -> Sequential: model = Sequential() model.add(Rescaling(scale=(1.0 / 255.0), offset=0.0)) return model
import numpy as np import tensorflow as tf from tensorflow import keras from tensorflow.keras.layers.experimental.preprocessing import CenterCrop from tensorflow.keras.layers.experimental.preprocessing import Rescaling #build a CNN dense = keras.layers.Dense(units=16) # Let's say we expect our inputs to be RGB images of arbitrary size inputs = keras.Input(shape=(None, None, 3)) from tensorflow.keras import layers # Center-crop images to 101 x 200 to fit sample size x = CenterCrop(height=101, width=200)(inputs) # Rescale images to [0, 1] x = Rescaling(scale=1./255)(x) # Apply some convolution and pooling layers x = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='SAME', activation='relu')(x) x = layers.MaxPooling2D(pool_size=(3, 3))(x) x = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='SAME', activation='relu')(x) # Apply global average pooling to get flat feature vectors x = layers.GlobalAveragePooling2D()(x) # add a dense layer x = layers.Dense(20, activation='relu')(x) # Add a dense classifier on top num_classes = 10 outputs = layers.Dense(num_classes, activation='softmax')(x) model = keras.Model(inputs=inputs, outputs=outputs) model.summary() #compile and keep metrics model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',metrics=[keras.metrics.SparseCategoricalAccuracy(name='acc')])
batch_size = batch_size, ) # small sample for testing #validation_images = validation_images.sample(500) val_x = load_imgs_from_df(validation_images, x_col='file', img_dir=image_dir, target_size=target_size, data_format='channels_last') val_y = {c:to_categorical(validation_images[c]) for c in output_classes.keys()} #------------------------- # The keras model #------------------------- # basiccnn from https://towardsdatascience.com/different-ways-of-improving-training-accuracy-c526db15a5b2 base_model = keras.Sequential() base_model.add(Input(shape=target_size + (3,))) base_model.add(Rescaling(scale = 1./127.5)) base_model.add(keras.layers.Conv2D(16, kernel_size=(3, 3), padding='same', strides=(2, 2), activation='relu', name='conv1a')) base_model.add(keras.layers.Conv2D(16, kernel_size=(3, 3), padding='same', strides=(2, 2), activation='relu', name='conv1b')) base_model.add(keras.layers.MaxPooling2D((2,2), strides=(2,2), padding='same', name='max_pool1')) base_model.add(keras.layers.Conv2D(32, kernel_size=(3, 3), padding='same', strides=(2, 2), activation='relu', name='conv2a')) base_model.add(keras.layers.Conv2D(32, kernel_size=(3, 3), padding='same', strides=(2, 2), activation='relu', name='conv2b')) base_model.add(keras.layers.MaxPooling2D((2,2), strides=(2,2), padding='same', name='max_pool2')) base_model.add(keras.layers.Conv2D(64, kernel_size=(3, 3), padding='same', strides=(2, 2), activation='relu', name='conv3a')) base_model.add(keras.layers.Conv2D(64, kernel_size=(3, 3), padding='same',
leny2 = np.random.randint(3, 7) xx[y2 - leny2:y2 + leny2, x2 - lenx2:x2 + lenx2, :] = rand_color() xx[y2 - 12:y2, x2 - 3:x2 + 3, :] = [0,0,0] #plt.figure() #plt.imshow(xx) #plt.show() X_List.append(xx) return np.array(X_List), np.array(Y_List) model = keras.Sequential( [ keras.Input(shape=(H, W, C)), Rescaling(scale=1.0 / 255), layers.Conv2D(32, kernel_size=(3, 3), activation='relu'), layers.MaxPooling2D(pool_size=(2, 2)), layers.Dropout(0.1), layers.Conv2D(32, kernel_size=(3, 3), activation='relu'), layers.MaxPooling2D(pool_size=(2, 2)), layers.Dropout(0.1), layers.Conv2D(32, kernel_size=(3, 3), activation='relu'), layers.MaxPooling2D(pool_size=(2, 2)), layers.Dropout(0.1), layers.Flatten(), layers.Dense(128, activation='relu'), layers.Dense(128, activation='relu'), layers.Dense(32), layers.Dropout(0.3), layers.Dense(1),
# Fiting dataset images = imt.load_dataset('/data/gee/SAR/transformer/data/train/c2', img_height, img_width) train_dataset = tf.data.Dataset.from_tensor_slices((images, images)) train_dataset = train_dataset.shuffle(10).batch(batch_size) # End Custom dataset strategy = tf.distribute.MirroredStrategy() with strategy.scope(): model = tf.keras.models.Sequential() model.add(Rescaling(1.0 / 255)) model.add( VisionTransformer( image_size=args.image_size, patch_size=args.patch_size, num_layers=args.num_layers, num_classes=10, d_model=args.d_model, num_heads=args.num_heads, mlp_dim=args.mlp_dim, channels=3, dropout=0.1, )) model.add(Dense(args.mlp_dim, activation=tfa.activations.gelu))
def __call__(self, inputs): """ takes in data in list format & returns its encoded form """ x = Rescaling(scale=1.0 / 255)(inputs) return x
def __init__(self, img_height=128, img_width=128, img_channels=3, embedding_size=64, return_spatial_softmax=False, l1=16, l2=32, l3=32): super(impala_cnn, self).__init__() self.img_height = img_height self.img_width = img_width self.img_channels = img_channels self.rescaling = Rescaling( 1. / 255, input_shape=(img_height, img_width, img_channels)) # put it here for portability self.conv_1 = Conv2D(l1, 3, strides=(2, 2), padding='same', activation='relu', name='c1') self.res_1_1 = Conv2D(l1, 3, strides=(1, 1), padding='same', activation='relu', name='r1_1') self.res_1_2 = Conv2D(l1, 3, strides=(1, 1), padding='same', activation='relu', name='r1_2') self.conv_2 = Conv2D(l2, 3, strides=(2, 2), padding='same', activation='relu', name='c2') self.res_2_1 = Conv2D(l2, 3, strides=(1, 1), padding='same', activation='relu', name='r2_1') self.res_2_2 = Conv2D(l2, 3, strides=(1, 1), padding='same', activation='relu', name='r2_2') self.conv_3 = Conv2D(l3, 3, strides=(2, 2), padding='same', activation='relu', name='c3') self.res_3_1 = Conv2D(l3, 3, strides=(1, 1), padding='same', activation='relu', name='r3_1') self.res_3_2 = Conv2D(l3, 3, strides=(1, 1), padding='same', activation='relu', name='r3_2') # In between these, do a spatial softmax self.flatten = Flatten() self.dense1 = Dense(256, activation='relu') self.dense2 = Dense(embedding_size) self.return_spatial_softmax = return_spatial_softmax
from tensorflow.keras.applications import ResNet50 from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Input from tensorflow.keras.layers.experimental.preprocessing import Rescaling # create resnet model w imgnet weights resnet_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3), pooling= None) resnet_model.summary() model= Sequential() model.add(Rescaling(1./255, input_shape=(224, 224, 3))) for layer in resnet_model.layers: layer.trainable = True model.add(resnet_model) model.add(GlobalAveragePooling2D()) model.add(Dense(1, activation='sigmoid')) #model.load_weights(r'E:\Babette\MasterThesis\Models\ResNet_imgnet_trainable_full\resnet_model1_finetuned.h5') model.load_weights(r'resnet_model1_finetuned.h5') model.summary() input_layer = Input(batch_shape=model.layers[1].input_shape) prev_layer = input_layer for layer in model.layers: layer._inbound_nodes = [] prev_layer = layer(prev_layer)
) # Model training model.fit(x_train, y_train, epochs = 1, validation_split=0.1, batch_size=16) """ In general, it's a good practice to develop models that take raw data as input, as opposed to models that take already-preprocessed data. The reason being that, if your model expects preprocessed data, any time you export your model to use it elsewhere (in a web browser, in a mobile app), you'll need to reimplement the same exact preprocessing pipeline. This can be a bit tricky to do. """ # normalize in range [0, 1] scaling_layer = Rescaling(1.0 / 255) # normalize in range [-1, 1] input_ = tf.keras.Input(shape=(32, 32, 3)) norm_neg_one_to_one = Normalization() x = norm_neg_one_to_one(input_) import numpy as np mean = [127.5]*3 var = mean ** 2 norm_neg_one_to_one.set_weights([mean, var]) norm_neg_one_to_one.get_weights() # normalize with mean 0 and std 1 norm_mean_std = Normalization() norm_mean_std.adapt(x_train[0]) model_ = Sequential([
def create_optpresso_model(input_shape: List) -> Sequential: model = Sequential() model.add(InputLayer(input_shape=input_shape)) model.add(SubtractMeanLayer(mean=MEAN_IMG_VALUES)) model.add(Rescaling(1.0 / 255)) model.add(RandomFlip()) model.add(RandomRotation(1)) model.add(Convolution2D( 32, (5, 5), padding="same", )) model.add(BatchNormalization()) model.add(Activation("relu")) # model.add(SpatialDropout2D(0.3)) model.add(Convolution2D( 48, (5, 5), strides=(2, 2), padding="same", )) # model.add(BatchNormalization()) # model.add(SpatialDropout2D(0.3)) model.add(Activation("relu")) model.add(Convolution2D( 48, (5, 5), strides=(2, 2), padding="same", )) # model.add(SpatialDropout2D(0.1)) model.add(Activation("relu")) model.add(Convolution2D( 64, (3, 3), strides=(2, 2), padding="same", )) # model.add(SpatialDropout2D(0.1)) model.add(Activation("relu")) model.add(Convolution2D( 64, (3, 3), strides=(2, 2), padding="same", )) model.add(Activation("relu")) model.add(Convolution2D( 128, (3, 3), strides=(2, 2), padding="same", )) # model.add(SpatialDropout2D(0.15)) model.add(Activation("relu")) model.add(Convolution2D( 128, (3, 3), strides=(2, 2), padding="same", )) model.add(Flatten()) model.add(Activation("relu")) model.add(Dense(128)) model.add(Dropout(0.5)) model.add(Activation("relu")) model.add(Dense(96)) model.add(Dropout(0.5)) model.add(Activation("relu")) model.add(Dense(64)) model.add(Dropout(0.5)) model.add(Activation("relu")) model.add(Dense(1, bias_initializer=Constant(MEAN_PULL_TIME))) return model
#validation_images = validation_images.sample(500) val_x = load_imgs_from_df(validation_images, x_col='filepath', img_dir='', target_size=target_size, data_format='channels_last') val_y = { c: to_categorical(validation_images[c]) for c in output_classes.keys() } #------------------------- # The keras model #------------------------- input_layer = Input(shape=target_size + (3, )) input_layer = Rescaling(scale=1. / 127.5, offset=-1)(input_layer) base_model = keras.applications.VGG16( weights=None, # Load weights pre-trained on ImageNet. #input_shape= target_size + (3,), input_tensor=input_layer, include_top=False, ) # Do not include the ImageNet classifier at the top. def build_category_model(prior_step, class_n, name): x = keras.layers.Flatten()(prior_step) x = keras.layers.Dense(4096, activation='relu')(x) x = keras.layers.Dropout(0.5)(x) x = keras.layers.Dense(4096, activation='relu')(x) x = keras.layers.Dropout(0.5)(x)
import numpy as np import tensorflow as tf from tensorflow import keras from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten from tensorflow.keras.layers.experimental.preprocessing import Rescaling # Load dataset test_data_set = keras.preprocessing.image_dataset_from_directory( './testing', batch_size=64, image_size=(28, 28)) train_data_set = keras.preprocessing.image_dataset_from_directory( './training', batch_size=64, image_size=(28, 28)) # Define variables to use in the model scaling = Rescaling(scale=1. / 255) num_filters = 32 filter_size = 3 pool_size = 2 num_classes = 10 # Define the model model = Sequential([ scaling, Conv2D(num_filters, filter_size), MaxPooling2D(), Conv2D(num_filters, filter_size), MaxPooling2D(), Conv2D(num_filters, filter_size), MaxPooling2D(), Flatten(), Dense(128, activation='relu'),