{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nfor dirname, _, _ in os.walk('/kaggle/input'):\n    print(dirname)\n\nfor files in os.listdir('/kaggle/input/aptos2019-blindness-detection'):\n    print(files)\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"names = []\nfor filenames in os.listdir('/kaggle/input/aptos2019-blindness-detection/train_images'):\n    names.append(filenames)\n    \nlen(names)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"traindf = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')\ntraindf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"traindf['id_code'] = [(i + '.png') for i in traindf['id_code']]\ntraindf['diagnosis'] = traindf['diagnosis'].astype(str)\nfor i in traindf.head(5)['id_code']:\n    print(i)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"traindf.iloc[[1,3,4],:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#This is your generator for the full dataset\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\ntrain_datagen = ImageDataGenerator(\n        rescale=1./255,\n        shear_range=0.2,\n        zoom_range=0.2,\n        horizontal_flip=True)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n        dataframe=traindf,\n        directory='/kaggle/input/aptos2019-blindness-detection/train_images',\n        x_col=\"id_code\",\n        y_col=\"diagnosis\",\n        target_size=(1000, 1000),\n        batch_size=32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Splitting the names into 5 folds\nfrom sklearn.model_selection import KFold\nkf = KFold(n_splits=5)\nkf.get_n_splits(traindf['id_code'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for train_index, test_index in kf.split(traindf['id_code']):\n    #Uncomment if you want train and test\n    #X_train, X_test = traindf.iloc[train_index], traindf.iloc[test_index]\n    X_train = traindf.iloc[train_index]\n    xtrain_generator = train_datagen.flow_from_dataframe(\n        dataframe=X_train,\n        directory='/kaggle/input/aptos2019-blindness-detection/train_images',\n        x_col=\"id_code\",\n        y_col=\"diagnosis\",\n        target_size=(1000, 1000),\n        batch_size=32)\n    \n    models_list = []\n    #Add your models here.\n    base_model = tf.keras.applications.inception_v3.InceptionV3(weights='imagenet', include_top=False)\n    x = base_model.output\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    x = tf.keras.layers.Dense(1024, activation='relu')(x)\n    predictions = tf.keras.layers.Dense(5, activation='softmax')(x)\n    for layer in base_model.layers:\n        layer.trainable = False\n\n    model = tf.keras.Model(inputs=base_model.input, outputs=predictions)\n    ###\n    ###\n    ### Uncomment the following lines to train your models\n    #model.compile(optimizer='adam', loss='categorical_crossentropy')\n    #model.fit_generator(xtrain_generator, steps_per_epoch=100, epochs=50)\n    models_list.append(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Just iterate through all models in models_list to and take their mean for final prediction\nmodels_list[0].predict(np.zeros((1,1000,1000,3)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}