{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from glob import glob\nimport pickle\nfrom tqdm.notebook import tqdm\nimport pydicom as dicom\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport os\nfrom tensorflow.keras.preprocessing.image import img_to_array, load_img\nimport warnings\nimport matplotlib.pyplot as plt\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_top_images_with_lables(images, lables, plot_random=True):\n  columns = 4\n  rows = 5\n  fig=plt.figure(figsize=(12, 17))\n  for i in range(1, columns*rows +1):\n    index = None\n    if plot_random == True:\n      index = randint(0, (len(lables)-1))\n    else:\n      index = i\n    fig.add_subplot(rows, columns, i).set_title(lables[index-1])\n    plt.imshow(images[index-1], cmap='gray')\n  return plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dcm_path = '../input/rsna-pneumonia-detection-challenge/stage_2_train_images/*'\ntest_dcm_path = '../input/rsna-pneumonia-detection-challenge/stage_2_test_images/*'\ntrain_class_csv_path = '../input/rsna-pneumonia-detection-challenge/stage_2_detailed_class_info.csv'\ntrain_lable_csv_path = '../input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_lables_df = pd.read_csv(train_lable_csv_path)\ntrain_lables_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_lable_class_df = pd.read_csv(train_class_csv_path)\ntraining_lable_class_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dcm_glop = glob(train_dcm_path)\ntest_dcm_glop = glob(test_dcm_path)\nlen(train_dcm_glop), len(test_dcm_glop)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = (224,224)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_image_data = []\ntraining_image_names = []\nprint(\"Reading Images....\")\nfor train_dcm_path in train_dcm_glop:\n    with tf.device('/device:GPU:0'):\n        file_name = os.path.split(train_dcm_path)[1]\n        ds = dicom.dcmread(train_dcm_path)\n        image = np.array(ds.pixel_array)\n        image = cv2.resize(image, image_size)\n        training_image_data.append(image)\n        training_image_names.append(file_name)\nprint(\"Completed.....\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(training_image_names), len(training_image_data)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_image_data[0].shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_name = [i+1 for i in range(0,20)]\nlen(file_name)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_top_images_with_lables(training_image_data, training_image_names, False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_image_data = np.array(training_image_data)\ntraining_image_names = np.array(training_image_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_image_data.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_image_names.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testing_image_data = []\ntesting_image_names = []\nprint(\"Reading Images....\")\nfor test_dcm_path in test_dcm_glop:\n    with tf.device('/device:GPU:0'):\n        file_name = os.path.split(test_dcm_path)[1]\n        ds = dicom.dcmread(test_dcm_path)\n        image = np.array(ds.pixel_array)\n        image = cv2.resize(image, image_size)\n        testing_image_data.append(image)\n        testing_image_names.append(file_name)\nprint(\"Completed.....\")\ntesting_image_data = np.array(testing_image_data)\ntesting_image_names = np.array(testing_image_names)\ntesting_image_data.shape, testing_image_names.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_top_images_with_lables(testing_image_data, testing_image_names, False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_dict = {\n    \"TrainingImagesData\": training_image_data,\n    \"TrainingImageNames\": training_image_names,\n    \"TestingImageData\": testing_image_data,\n    \"TestImageNames\": testing_image_names\n}\n\ndata_pickle_file = open('RSNA-Pneumonia-Detection-Chalenge.pkl', 'ab')\npickle.dump(dataset_dict, data_pickle_file)\ndata_pickle_file.close() ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_lables_df.to_pickle(\"RSNA-Pneumonia-Detection-Chalenge_lables.pkl\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\nFileLink(r'RSNA-Pneumonia-Detection-Chalenge_lables.pkl')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FileLink(r'RSNA-Pneumonia-Detection-Chalenge.pkl')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_lable_class_df.to_pickle(\"RSNA-Pneumonia-Detection-Chalenge_lables_classes.pkl\")\nFileLink(r'RSNA-Pneumonia-Detection-Chalenge_lables_classes.pkl')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}