tensorflow / tpu

Reference models and tools for Cloud TPUs.
https://cloud.google.com/tpu/
Apache License 2.0
5.21k stars 1.77k forks source link

how to use mixnet? #800

Open mobassir94 opened 4 years ago

mobassir94 commented 4 years ago

i am unable to use mixnet in kaggle kernel, there is no pip install command to install this model in colab/kaggle or in local pc for tensorflow 2.0+ i copied code and tried to load mixnet like this : build_model(ds_train,'mixnet-l',training = True)

but it gives me this error :


AssertionError Traceback (most recent call last)

in ----> 1 build_model(ds_train,'mixnet-l',training = True) in build_model(images, model_name, training, override_params) 242 When override_params has invalid fields, raises ValueError. 243 """ --> 244 assert isinstance(images, tf.Tensor) 245 blocks_args, global_params = get_model_params(model_name, override_params) 246 tf.logging.info('blocks_args= {}'.format(blocks_args)) AssertionError: inside ds_train i have this :
mingxingtan commented 4 years ago

Looks like your ds_train has two fields with type <tf.float32, (tf.int64)>, which possibly includes both train images and labels.

the current build_model only accept images for the first argument.

mobassir94 commented 4 years ago

@mingxingtan thanks

yes it does contain both images and labels,it is tfrecord file can you help me a bit? how do i change ds_train to use mixnet model? using timm i used mixnet in pytorch but i am not finding any good and easy way of using this in tensorflow

here is how i use my data in Xception model using this notebook : https://www.kaggle.com/mobassir/in-depth-melanoma-with-modeling/data

please check the code :

GCS_PATH2    = KaggleDatasets().get_gcs_path('isic2019-256x256')
files_train1 = tf.io.gfile.glob(GCS_PATH2 + '/train*.tfrec')
files_test  = np.sort(np.array(tf.io.gfile.glob(GCS_PATH + '/test*.tfrec')))

from tensorflow.keras.layers import GlobalAveragePooling2D
from tensorflow.keras.applications import Xception
def create_model(input_shape, n_out):
    input_tensor = Input(shape=input_shape)
    base_model = Xception(include_top=False,
                   weights='imagenet',
                   input_tensor=input_tensor)

    # https://towardsdatascience.com/multi-sample-dropout-in-keras-ea8b8a9bfd83

    x = GlobalAveragePooling2D()(base_model.output)

    dense = []
    for p in np.linspace(0.3,0.5, 5):
        x = Dense(1024, activation='relu')(x)
        x_ = tf.keras.layers.Dropout(p)(x)
        dense.append(x_)
    x = tf.keras.layers.Average()(dense)

    final_output = Dense(n_out, activation='sigmoid', name='final_output')(x)
    model = Model(input_tensor, final_output)

    return model

model = create_model(
    input_shape=(256,256,3), 
    n_out=1)
model.compile(
            optimizer = CFG['optimizer'],
            loss      =  tf.keras.losses.BinaryCrossentropy(label_smoothing = CFG['label_smooth_fac']),
            metrics   = [tf.keras.metrics.AUC(name='auc')])

ds_train     = get_dataset(files_train1, CFG, augment=True, shuffle=True, repeat=True)
unpack_label = lambda img, label: (img, tuple([label]))
unpack_label = tf.autograph.experimental.do_not_convert(unpack_label)  # Runtime not compatible
ds_train = ds_train.map(unpack_label)

steps_train  = count_data_items(files_train1) / (CFG['batch_size'] * REPLICAS)

history_Xception1   = model.fit(ds_train, 
                         verbose          = 1,
                         steps_per_epoch  = steps_train, 
                         epochs           = 20,
                         callbacks        = [get_lr_callback(CFG)])

now you see how i am using my data for Xception model,can you tell me how do i modify my baseline for using mixnet models?