{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import cv2\nimport pandas as pd\nimport keras\nfrom keras.applications.inception_v3 import InceptionV3\nfrom keras.preprocessing import image\nfrom keras.models import Model\nfrom keras.layers import Input, Dense, GlobalAveragePooling2D\nfrom keras import backend as K\nimport os\nimport numpy as np\nfrom numpy.random import seed\nimport json\nfrom collections import Counter\nfrom keras.optimizers import SGD\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nfrom IPython.display import Image as Image2\nfrom IPython.display import display\nfrom matplotlib.pyplot import imshow\nimport urllib\nimport random\nimport tensorflow as tf\nfrom scipy.spatial import distance_matrix\nfrom glob import glob\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.python.client import device_lib\nprint(device_lib.list_local_devices())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images_glob = glob('../input/data/images_*/images/*.png')\nprint(f'Number of images {len(images_glob)}')\nxray_data = pd.read_csv('../input/data/Data_Entry_2017.csv')\nimage_paths = { os.path.basename(path): path for path in images_glob }\nxray_data['full_path'] = xray_data['Image Index'].map(image_paths.get)\nnih_paths = list(xray_data['full_path'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_paths = list(glob('../input/ranzcr-clip-catheter-line-classification/train/*.jpg'))\ntest_paths = list(glob('../input/ranzcr-clip-catheter-line-classification/test/*.jpg'))\nsample_images = list(nih_paths + train_paths +test_paths)\nsample_images =sample_images[:50000]\nprint(f'length of nih paths is {len(nih_paths)}')\nprint(f'length of train paths is {len(train_paths)}')\nprint(f'length of test paths is {len(test_paths)}')\nprint(f'length of all images is {len(sample_images)}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\nseed_everything(42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# trainable = False is import because we'll be using this system for features but not output\n\nbase_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(1024, 1024, 3))\nfor layer in base_model.layers:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pool_2d = GlobalAveragePooling2D(name='pool_2d')(base_model.output)\ndense1 = Dense(1024, name='dense_1', activation='relu')(pool_2d)\ndense2 = Dense(2048, name='dense_2', activation='relu')(dense1)\npredictions = Dense(1024, activation='relu', name='dense_3')(dense2)\nmodel = Model(inputs=base_model.input, outputs=predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output_list = np.arange(0,1024 ).reshape(1, 1024)\nfor c in tqdm(sample_images):\n    use_images = cv2.imread(c)\n    use_images =cv2.resize(use_images, (1024, 1024))\n    use_images = use_images.reshape((1,1024, 1024, 3))\n    model_output = model.predict(use_images, batch_size=1, verbose=0)\n    output_list =np.concatenate((output_list, model_output), axis=0) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output_list= output_list[1:]\noutput_list.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df1 = pd.DataFrame(output_list, index = sample_images)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df1.to_csv('from0to50k.csv')","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":4}