{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":23870,"databundleVersionId":1781260,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Data preprocessing","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n\n# Load CSV files\ntrain_df = pd.read_csv('/kaggle/input/ranzcr-clip-catheter-line-classification/train.csv')\nannotations_df = pd.read_csv('/kaggle/input/ranzcr-clip-catheter-line-classification/train_annotations.csv')\n\n# Check the column names of annotations_df\nprint(annotations_df.columns)\n\n# Merge dataframes\nmerged_df = train_df.merge(annotations_df, on='StudyInstanceUID', how='left')\n\n# Extract features\nfeatures = merged_df[['ETT - Abnormal', 'ETT - Borderline',\n                      'ETT - Normal', 'NGT - Abnormal', 'NGT - Borderline',\n                      'NGT - Incompletely Imaged', 'NGT - Normal', \n                      'CVC - Abnormal', 'CVC - Borderline', \n                      'CVC - Normal', 'Swan Ganz Catheter Present', \n                      'PatientID']].values\n\n# Extract labels\nlabels = merged_df['label'].values  # This should be fine\n\n# View the merged data\nprint(merged_df.head())\n\nmerged_df = merged_df.dropna(subset=['label'])  # Remove rows with NaN\n\nprint(merged_df['label'].head())  # View the first few labels\nprint(merged_df['label'].unique())  # View unique values\n\nfrom sklearn.preprocessing import LabelEncoder\n\nlabel_encoder = LabelEncoder()\nlabels = label_encoder.fit_transform(merged_df['label'].values)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T06:12:38.256818Z","iopub.execute_input":"2025-05-15T06:12:38.257036Z","iopub.status.idle":"2025-05-15T06:12:40.242896Z","shell.execute_reply.started":"2025-05-15T06:12:38.257019Z","shell.execute_reply":"2025-05-15T06:12:40.242146Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1. Load and process JPEG files\nfrom PIL import Image\nimport numpy as np\n\n# Suppose merged_df is your DataFrame and the StudyInstanceUID you are going to load is that of the first patient\nstudy_instance_uid = merged_df['StudyInstanceUID'].iloc[0] # Get the first StudyInstanceUID\nimage_path = f'/kaggle/input/ranzcr-clip-catheter line-classification/train/{study_instance_uid}.jpg' # Build the path using formatted strings\n\n# Load JPEG images\nImage = image.open (image_path).convert('RGB') # Convert to RGB\nimage = Image.resize ((224, 224)) # resize\nimage_array = np.array(image) / 255.0 # Normalization","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T06:32:58.754475Z","iopub.execute_input":"2025-05-14T06:32:58.754897Z","iopub.status.idle":"2025-05-14T06:32:58.78281Z","shell.execute_reply.started":"2025-05-14T06:32:58.754817Z","shell.execute_reply":"2025-05-14T06:32:58.782014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1. Load and process the first 1000 JPEG files\nfrom PIL import Image\nimport numpy as np\n\n# Suppose merged_df is your DataFrame\nnum_samples = min(1000, len(merged_df)) # Take the total number of samples of the first 1000 or dataframes\nimage_array = np.zeros((num_samples, 224, 224, 3)) # Initialize the array\n\n# Load each image\nfor i in range(num_samples):\n    study_instance_uid = merged_df['StudyInstanceUID'].iloc[i] # Get the current StudyInstanceUID\n    image_path = f'/kaggle/input/ranzcr-clip-catheter-line classification/train/{study_instance_uid}.jpg' # Build the path\n    \n    # Load JPEG images\n    Image = image.open (image_path).convert('RGB') # Convert to RGB\n    image = Image.resize ((224, 224)) # resize\n    image_array[i] = np.array(image) / 255.0 # Normalize and store in the array\n\n# Now the image_array contains the first 1000 images\nprint(image_array.shape) # should be (num_samples, 224, 224, 3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T06:39:18.464203Z","iopub.execute_input":"2025-05-15T06:39:18.464466Z","iopub.status.idle":"2025-05-15T06:40:28.281361Z","shell.execute_reply.started":"2025-05-15T06:39:18.464447Z","shell.execute_reply":"2025-05-15T06:40:28.280754Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# The above ones are for all the patients, and this one is for the patients with anchor boxes.\nimport pandas as pd\n\n# Load the CSV file\ntrain_df = pd.read_csv('/kaggle/input/ranzcr-clip-catheter-line-classification/train.csv')\nannotations_df = pd.read_csv('/kaggle/input/ranzcr-clip-catheter-line-classification/train_annotations.csv')\n\n# View the column names of annotations_df\nprint(annotations_df.columns)\n\n# Merge the data frame mainly with annotations_df\nmerged_df = annotations_df.merge(train_df, on='StudyInstanceUID', how='left')\n\n# Extract Features\ncsv_features = merged_df[['ETT - Abnormal', 'ETT - Borderline',\n                        'ETT - Normal', 'NGT - Abnormal', 'NGT - Borderline',\n                        'NGT - Incompletely Imaged', 'NGT - Normal',\n                        'CVC - Abnormal', 'CVC - Borderline',\n                        'CVC - Normal', 'Swan Ganz Catheter Present',\n                        ]].values\n\n# Extract Tags\nlabels = merged_df['label'].values # There should be no problem here\n\nfrom sklearn.preprocessing import LabelEncoder\n\n# Suppose labels is an array containing strings\nlabel_encoder = LabelEncoder()\nlabels = label_encoder.fit_transform(labels) # Convert to numerical labels\n\n# Make sure that the converted type is numeric\nprint(labels)\nprint(labels.dtype) # should be int\n\n\n# View the merged data\nprint(merged_df.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T04:48:32.096394Z","iopub.execute_input":"2025-05-15T04:48:32.097095Z","iopub.status.idle":"2025-05-15T04:48:32.275273Z","shell.execute_reply.started":"2025-05-15T04:48:32.097067Z","shell.execute_reply":"2025-05-15T04:48:32.274506Z"},"scrolled":true,"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# Suppose merged_df is your DataFrame\n# Extract the first 1000 lines\nsmall_dataset = merged_df.head(1000)\n\n# Extract Features\ncsv_features = merged_df[['ETT - Abnormal', 'ETT - Borderline',\n                        'ETT - Normal', 'NGT - Abnormal', 'NGT - Borderline',\n                        'NGT - Incompletely Imaged', 'NGT - Normal',\n                        'CVC - Abnormal', 'CVC - Borderline',\n                        'CVC - Normal', 'Swan Ganz Catheter Present']].head(1000).values\n\n# Calculate the number of samples and features\nnum_samples = csv_features.shape[0] # Sample quantity\nnum_csv_features = csv_features.shape[1] # Number of features\n\n# Print Shape\nprint(f\" Sample quantity: {num_samples}, feature quantity: {num_csv_features}\")\n\n# Save as a new CSV file\nsmall_dataset.to_csv('small_dataset.csv', index=False)\n\n# Print the result for confirmation\nprint(small_dataset.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T04:48:35.40914Z","iopub.execute_input":"2025-05-15T04:48:35.409774Z","iopub.status.idle":"2025-05-15T04:48:35.436536Z","shell.execute_reply.started":"2025-05-15T04:48:35.40975Z","shell.execute_reply":"2025-05-15T04:48:35.435854Z"},"scrolled":true,"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extract the features of the first 1000 samples\ncsv_features = merged_df[['ETT - Abnormal', 'ETT - Borderline',\n                            'ETT - Normal', 'NGT - Abnormal', 'NGT - Borderline',\n                            'NGT - Incompletely Imaged', 'NGT - Normal',\n                            'CVC - Abnormal', 'CVC - Borderline',\n                            'CVC - Normal', 'Swan Ganz Catheter Present']].iloc[:1000]\n\n# Make sure to extract only the numerical part\ncsv_features = cSV_features.values # Convert to a NumPy array\n\n# Print to confirm the data type\nprint(csv_features)\nprint(csv_features.dtype) # Make sure it is a numeric type (such as int or float)\n\n# Calculate the number of samples and features\nnum_samples = csv_features.shape[0] # should be 1000\nnum_csv_features = csv_features.shape[1] # should be 10\n\n# Print Shape\nprint(f\" Sample quantity: {num_samples}, feature quantity: {num_csv_features}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T06:16:51.042242Z","iopub.execute_input":"2025-05-15T06:16:51.042501Z","iopub.status.idle":"2025-05-15T06:16:51.051267Z","shell.execute_reply.started":"2025-05-15T06:16:51.04248Z","shell.execute_reply":"2025-05-15T06:16:51.050606Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Merge image and CSV features\ncombined_input = [image_array[np.newaxis,...], csv_features] # Add a new dimension\n\n# Ensure that the shape of combined_input is correct\nprint(image_array.shape) # should be (224, 224, 3)\nprint(csv_features.shape) # should be (num_samples, num_csv_features)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T06:40:49.049378Z","iopub.execute_input":"2025-05-15T06:40:49.050139Z","iopub.status.idle":"2025-05-15T06:40:49.05454Z","shell.execute_reply.started":"2025-05-15T06:40:49.050118Z","shell.execute_reply":"2025-05-15T06:40:49.053765Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"merged_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T06:10:15.198218Z","iopub.execute_input":"2025-05-14T06:10:15.198491Z","iopub.status.idle":"2025-05-14T06:10:15.212919Z","shell.execute_reply.started":"2025-05-14T06:10:15.19845Z","shell.execute_reply":"2025-05-14T06:10:15.212278Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"merged_df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T06:08:42.098775Z","iopub.execute_input":"2025-05-14T06:08:42.099045Z","iopub.status.idle":"2025-05-14T06:08:42.104234Z","shell.execute_reply.started":"2025-05-14T06:08:42.099Z","shell.execute_reply":"2025-05-14T06:08:42.103567Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Initial Architecture\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\n\n# Suppose we have already processed the image data and CSV features\n# Processed image data\nimage_input = layers.Input(shape=(224, 224, 3), name='image_input')\nimage_features = layers.Conv2D(32, (3, 3), activation='relu')(image_input)\nimage_features = layers.MaxPooling2D()(image_features)\nimage_features = layers.Flatten()(image_features)\n\n# Suppose some numerical features have been extracted from CSV\ncsv_input = layers.Input(shape=(num_csv_features,), name='csv_input') # num_csv_features represents the number of CSV features\n\n# Merge image features and CSV features\nmerged = layers.Concatenate()([image_features, csv_input])\n\n# Continue to build the model\ndense = layers.Dense(64, activation='relu')(merged)\noutput = layers.Dense(len(merged_df['label'].unique()), activation='softmax')(dense) # Adjust according to the number of labels\n\n# Create a Model\nmodel = models.Model(inputs=[image_input, csv_input], outputs=output)\n\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T06:29:04.33426Z","iopub.execute_input":"2025-05-14T06:29:04.33457Z","iopub.status.idle":"2025-05-14T06:29:04.442035Z","shell.execute_reply.started":"2025-05-14T06:29:04.334516Z","shell.execute_reply":"2025-05-14T06:29:04.441354Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1. Adjust the architecture to correct the LOSS\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\n\n# Suppose we have already processed the image data and CSV features\nimage_input = layers.Input(shape=(224, 224, 3), name='image_input')\nimage_features = layers.Conv2D(32, (3, 3), activation='relu')(image_input)\nimage_features = layers.MaxPooling2D()(image_features)\nimage_features = layers.Flatten()(image_features)\n\n# Numerical features extracted from CSV\ncsv_input = layers.Input(shape=(num_csv_features,), name='csv_input') # num_csv_features represents the number of CSV features\n\n# Merge image features and CSV features\nmerged = layers.Concatenate()([image_features, csv_input])\n\n# Continue to build the model\ndense = layers.Dense(64, activation='relu')(merged)\noutput = layers.Dense(len(merged_df['label'].unique()), activation='softmax')(dense) # Adjust according to the number of labels\n\n# Create a Model\nmodel = models.Model(inputs=[image_input, csv_input], outputs=output)\n\n# Compilation Model\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T07:27:17.132443Z","iopub.execute_input":"2025-05-14T07:27:17.132733Z","iopub.status.idle":"2025-05-14T07:27:33.033754Z","shell.execute_reply.started":"2025-05-14T07:27:17.132711Z","shell.execute_reply":"2025-05-14T07:27:33.03316Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T07:27:33.034849Z","iopub.execute_input":"2025-05-14T07:27:33.035347Z","iopub.status.idle":"2025-05-14T07:27:33.053656Z","shell.execute_reply.started":"2025-05-14T07:27:33.035325Z","shell.execute_reply":"2025-05-14T07:27:33.052927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Finally, it can run, but the result seems to be problematic\n# Only extract the first 1000 tags\nlabels = labels[:1000] # Ensure that labels also only contain the first 1000 samples\n\n# Check the shape\nprint(csv_features.shape) # should be (1000, 11)\nprint(image_array.shape) # should be (1000, 224, 224, 3)\nprint(labels.shape) # should be (1000,) or (1000, num_classes)\n\n# Train the Model\nhistory = model.fit(\n    [image_array, csv_features], # Ensure that the sample numbers of these two arrays are consistent\n    labels,\n    epochs=2,\n    batch_size=2\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T07:27:43.553326Z","iopub.execute_input":"2025-05-14T07:27:43.553599Z","iopub.status.idle":"2025-05-14T07:27:59.294663Z","shell.execute_reply.started":"2025-05-14T07:27:43.553578Z","shell.execute_reply":"2025-05-14T07:27:59.294029Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Let's pause here for now. Currently, it's a mid-term fusion of csv and jpg. Next, we'll add a mid-term fusion of tf","metadata":{}},{"cell_type":"markdown","source":"### The previous code worked in 3.6.6 and now it also works in 3.11.12","metadata":{}},{"cell_type":"code","source":"# Write a function to parse TFRecord files and extract image and label data.\nimport tensorflow as tf\n\ndef _parse_function(proto):\n    # Define the parsing format\n    feature_description = {\n        'image_raw': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64),\n        # Add other features if needed\n    }\n    return tf.io.parse_single_example(proto, feature_description)\n\ndef load_tfrecord(filename):\n    # Read the TFRecord file\n    raw_dataset = tf.data.TFRecordDataset(filename)\n    parsed_dataset = raw_dataset.map(_parse_function)\n\n    images = []\n    labels = []\n    for parsed_record in parsed_dataset:\n        # Decode the image and resize it\n        image = tf.io.decode_raw(parsed_record['image_raw'], tf.uint8)\n        image = tf.image.resize(image, [224, 224])  # Resize to 224x224\n        images.append(image.numpy())\n        labels.append(parsed_record['label'].numpy())\n    \n    return images, labels\n\n\n# List all TFRecord files\ntfrecord_images, tfrecord_labels = load_tfrecord('/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/00-1881.tfrec')\n\n# Load all TFRecord files\ndataset = load_tfrecords(tfrecord_files)\n\n# Extract images and labels\nimages = []\nlabels = []\nfor parsed_record in dataset:\n    image = tf.io.decode_raw(parsed_record['image_raw'], tf.uint8)\n    image = tf.image.resize(image, [224, 224])  # Resize\n    images.append(image.numpy())\n    labels.append(parsed_record['label'].numpy())\n\n# Convert to NumPy arrays\nimages = np.array(images)\nlabels = np.array(labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T04:48:41.652539Z","iopub.execute_input":"2025-05-15T04:48:41.653222Z","iopub.status.idle":"2025-05-15T04:48:41.753332Z","shell.execute_reply.started":"2025-05-15T04:48:41.653196Z","shell.execute_reply":"2025-05-15T04:48:41.752185Z"},"collapsed":true,"jupyter":{"outputs_hidden":true,"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Don't know how long it will take\nimport tensorflow as tf\nimport numpy as np\n\ndef _parse_function(proto):\n    feature_description = {\n        'image_raw': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64),\n    }\n    return tf.io.parse_single_example(proto, feature_description)\n\ndef load_tfrecord(filename):\n    raw_dataset = tf.data.TFRecordDataset(filename)\n    parsed_dataset = raw_dataset.map(_parse_function)\n\n    images = []\n    labels = []\n    \n    for parsed_record in parsed_dataset:\n        image = tf.io.decode_raw(parsed_record['image_raw'], tf.uint8)\n        image = tf.reshape(image, [224, 224, 3])\n        images.append(image)\n        labels.append(parsed_record['label'])\n\n    return tf.stack(images), tf.convert_to_tensor(labels)\n\n# List all TFRecord files\ntfrecord_files = [f'/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/{i:02d}-1881.tfrec' for i in range(16)]\n\n# Load all TFRecord files\nall_images = []\nall_labels = []\n\nfor tfrecord_file in tfrecord_files:\n    images, labels = load_tfrecord(tfrecord_file)\n    all_images.append(images)  # Add images from each file\n    all_labels.append(labels)   # Add labels from each file\n\n# Merge all images and labels\nall_images = tf.concat(all_images, axis=0)\nall_labels = tf.concat(all_labels, axis=0)\n\n# Convert to NumPy arrays\nall_images = all_images.numpy()\nall_labels = all_labels.numpy()\n\n# Print shapes to confirm\nprint(all_images.shape)  # Should be (N, 224, 224, 3)\nprint(all_labels.shape)  # Should be (N,)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T07:08:20.10077Z","iopub.execute_input":"2025-05-14T07:08:20.101051Z","iopub.status.idle":"2025-05-14T07:09:03.801668Z","shell.execute_reply.started":"2025-05-14T07:08:20.10101Z","shell.execute_reply":"2025-05-14T07:09:03.800507Z"},"jupyter":{"outputs_hidden":true,"source_hidden":true},"collapsed":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Added a time display\nimport tensorflow as tf\nimport numpy as np\nimport time\n\ndef _parse_function(proto):\n    feature_description = {\n        'image_raw': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64),\n    }\n    return tf.io.parse_single_example(proto, feature_description)\n\ndef load_tfrecord(filename):\n    raw_dataset = tf.data.TFRecordDataset(filename)\n    parsed_dataset = raw_dataset.map(_parse_function)\n\n    images = []\n    labels = []\n    \n    for parsed_record in parsed_dataset:\n        image = tf.io.decode_raw(parsed_record['image_raw'], tf.uint8)\n        image = tf.reshape(image, [224, 224, 3])\n        images.append(image)\n        labels.append(parsed_record['label'])\n\n    return tf.stack(images), tf.convert_to_tensor(labels)\n\n# List all TFRecord files\ntfrecord_files = [f'/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/{i:02d}-1881.tfrec' for i in range(16)]\n\n# Load all TFRecord files\nall_images = []\nall_labels = []\n\ntotal_start_time = time.time()  # Record the start time\n\nfor tfrecord_file in tfrecord_files:\n    start_time = time.time()  # Record time before loading each file\n    images, labels = load_tfrecord(tfrecord_file)\n    all_images.append(images)  # Add images from each file\n    all_labels.append(labels)   # Add labels from each file\n    end_time = time.time()  # Record time after loading each file\n    print(f'Loaded {tfrecord_file} in {end_time - start_time:.2f} seconds.')\n\n# Merge all images and labels\nall_images = tf.concat(all_images, axis=0)\nall_labels = tf.concat(all_labels, axis=0)\n\n# Convert to NumPy arrays\nall_images = all_images.numpy()\nall_labels = all_labels.numpy()\n\n# Print shapes to confirm\nprint(all_images.shape)  # Should be (N, 224, 224, 3)\nprint(all_labels.shape)  # Should be (N,)\n\ntotal_end_time = time.time()  # Record the end time\nprint(f'Total loading time: {total_end_time - total_start_time:.2f} seconds.')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T07:09:53.660524Z","iopub.execute_input":"2025-05-14T07:09:53.660789Z","iopub.status.idle":"2025-05-14T07:11:05.613457Z","shell.execute_reply.started":"2025-05-14T07:09:53.660755Z","shell.execute_reply":"2025-05-14T07:11:05.612084Z"},"collapsed":true,"jupyter":{"outputs_hidden":true,"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd\n\ndef extract_features_from_tfrecords(tfrecord_files):\n    # List to store all data\n    all_data = []\n\n    for tfrecord_file in tfrecord_files:\n        # Create TFRecord dataset\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n\n        # Define parsing function\n        def _parse_function(proto):\n            # Define feature description\n            feature_description = {\n                'feature1': tf.io.FixedLenFeature([], tf.float32, default_value=0.0),\n                'feature2': tf.io.FixedLenFeature([], tf.float32, default_value=0.0),\n                # Add other features\n            }\n            return tf.io.parse_single_example(proto, feature_description)\n\n        # Parse dataset\n        parsed_dataset = raw_dataset.map(_parse_function)\n\n        # Extract features and add source information\n        for parsed_record in parsed_dataset:\n            record_data = {key: value.numpy() for key, value in parsed_record.items()}\n            record_data['source_file'] = tfrecord_file  # Add source file information\n            all_data.append(record_data)\n\n    # Create DataFrame\n    df = pd.DataFrame(all_data)\n    return df\n\n# Example usage\ntfrecord_files = [f'/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/{i:02d}-1881.tfrec' for i in range(16)]\ndf = extract_features_from_tfrecords(tfrecord_files)\nprint(df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T03:33:34.898171Z","iopub.execute_input":"2025-05-15T03:33:34.898707Z","iopub.status.idle":"2025-05-15T03:34:13.245773Z","shell.execute_reply.started":"2025-05-15T03:33:34.89868Z","shell.execute_reply":"2025-05-15T03:34:13.245027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for raw_record in raw_dataset.take(1):\n    example = tf.train.Example()\n    example.ParseFromString(raw_record.numpy())\n    print(example)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T07:32:58.519751Z","iopub.execute_input":"2025-05-14T07:32:58.520048Z","iopub.status.idle":"2025-05-14T07:32:58.557926Z","shell.execute_reply.started":"2025-05-14T07:32:58.520027Z","shell.execute_reply":"2025-05-14T07:32:58.557283Z"},"jupyter":{"source_hidden":true},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd\n\ndef extract_features_from_tfrecords(tfrecord_files):\n    # List to store all data\n    all_data = []\n\n    for tfrecord_file in tfrecord_files:\n        # Create TFRecord dataset\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n\n        # Define parsing function\n        def _parse_function(proto):\n            # Define feature description\n            feature_description = {\n                'StudyInstanceUID': tf.io.FixedLenFeature([], tf.string, default_value=''),\n                'CVC - Abnormal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'CVC - Borderline': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'CVC - Normal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'ETT - Abnormal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'ETT - Borderline': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'ETT - Normal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Abnormal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Borderline': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Incompletely Imaged': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Normal': tf.io.FixedLenFeature([], tf.int64, default_value=0),        \n                'Swan Ganz Catheter Present': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n            }\n            return tf.io.parse_single_example(proto, feature_description)\n\n        # Parse dataset\n        parsed_dataset = raw_dataset.map(_parse_function)\n\n        # Extract features and add source information\n        for parsed_record in parsed_dataset:\n            record_data = {key: value.numpy() for key, value in parsed_record.items()}\n            record_data['source_file'] = tfrecord_file  # Add source file information\n            all_data.append(record_data)\n\n    # Create DataFrame\n    df = pd.DataFrame(all_data)\n\n    # Adjust column order to place StudyInstanceUID at the front\n    cols = ['StudyInstanceUID'] + [col for col in df.columns if col != 'StudyInstanceUID']\n    df = df[cols]\n\n    return df\n\n# Example usage\ntfrecord_files = [f'/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/{i:02d}-1881.tfrec' for i in range(16)]\ndf = extract_features_from_tfrecords(tfrecord_files)\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T07:36:35.742139Z","iopub.execute_input":"2025-05-14T07:36:35.742814Z","iopub.status.idle":"2025-05-14T07:36:47.450853Z","shell.execute_reply.started":"2025-05-14T07:36:35.742789Z","shell.execute_reply":"2025-05-14T07:36:47.450126Z"},"jupyter":{"source_hidden":true},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Then match the first 1000 ids in my merged_df with the 1000 ids of this one. If they match, read the image from the tfrec file.","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd\n\ndef extract_features_from_tfrecords(tfrecord_files):\n    all_data = []\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n        \n        def _parse_function(proto):\n            feature_description = {\n                'StudyInstanceUID': tf.io.FixedLenFeature([], tf.string, default_value=''),\n                'CVC - Abnormal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'CVC - Borderline': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'CVC - Normal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'ETT - Abnormal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'ETT - Borderline': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'ETT - Normal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Abnormal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Borderline': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Incompletely Imaged': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Normal': tf.io.FixedLenFeature([], tf.int64, default_value=0),        \n                'Swan Ganz Catheter Present': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n            }\n            return tf.io.parse_single_example(proto, feature_description)\n\n        parsed_dataset = raw_dataset.map(_parse_function)\n        \n        for parsed_record in parsed_dataset:\n            record_data = {key: value.numpy() for key, value in parsed_record.items()}\n            # Convert StudyInstanceUID from byte string to regular string\n            record_data['StudyInstanceUID'] = record_data['StudyInstanceUID'].decode('utf-8')\n            all_data.append(record_data)\n\n    df = pd.DataFrame(all_data)\n    cols = ['StudyInstanceUID'] + [col for col in df.columns if col != 'StudyInstanceUID']\n    df = df[cols]\n    return df\n\n# Example usage\ntfrecord_files = [f'/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/{i:02d}-1881.tfrec' for i in range(16)]\ndf = extract_features_from_tfrecords(tfrecord_files)\n\n# Assuming merged_df is an existing DataFrame\nmerged_ids = merged_df['StudyInstanceUID'].head(1000).tolist()\n\n# Print IDs and their types\nprint(\"Merged IDs:\")\nprint(merged_ids[:5])\nprint(\"Types:\", [type(id) for id in merged_ids[:5]])\n\nextracted_ids = df['StudyInstanceUID'].tolist()\nprint(\"Extracted IDs:\")\nprint(extracted_ids[:5])\nprint(\"Types:\", [type(id) for id in extracted_ids[:5]])\n\n# Match IDs\nmatched_ids = set(merged_ids) & set(extracted_ids)\n\n# View match results\nprint(f'Matched IDs count: {len(matched_ids)}')\nprint(f'Matched IDs: {matched_ids}')\n\n# Load matched images\ndef load_image(image_id):\n    image_path = f'/path/to/images/{image_id}.png'  # Replace with the correct path\n    return tf.io.read_file(image_path)\n\n# For each matched ID, read the image\nfor image_id in matched_ids:\n    image_data = load_image(image_id)\n    image = tf.image.decode_png(image_data, channels=3)\n    print(f'Loaded image for ID: {image_id}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T07:41:34.296448Z","iopub.execute_input":"2025-05-14T07:41:34.297153Z","iopub.status.idle":"2025-05-14T07:41:46.23392Z","shell.execute_reply.started":"2025-05-14T07:41:34.297132Z","shell.execute_reply":"2025-05-14T07:41:46.232865Z"},"scrolled":true,"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd\n\ndef extract_features_from_tfrecords(tfrecord_files):\n    all_data = []\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n        \n        def _parse_function(proto):\n            feature_description = {\n                'StudyInstanceUID': tf.io.FixedLenFeature([], tf.string, default_value=''),\n                'CVC - Abnormal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'CVC - Borderline': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'CVC - Normal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'ETT - Abnormal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'ETT - Borderline': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'ETT - Normal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Abnormal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Borderline': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Incompletely Imaged': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Normal': tf.io.FixedLenFeature([], tf.int64, default_value=0),        \n                'Swan Ganz Catheter Present': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'image': tf.io.FixedLenFeature([], tf.string),  # Add image feature\n            }\n            return tf.io.parse_single_example(proto, feature_description)\n\n        parsed_dataset = raw_dataset.map(_parse_function)\n        for parsed_record in parsed_dataset:\n            record_data = {key: value.numpy() for key, value in parsed_record.items()}\n            record_data['StudyInstanceUID'] = record_data['StudyInstanceUID'].decode('utf-8')\n            all_data.append(record_data)\n\n    df = pd.DataFrame(all_data)\n    cols = ['StudyInstanceUID'] + [col for col in df.columns if col != 'StudyInstanceUID']\n    df = df[cols]\n    return df\n\n# Function to read images, extracting image data from TFRecord\ndef load_image_from_tfrecord(image_id, tfrecord_files):\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n        for raw_record in raw_dataset:\n            example = tf.train.Example()\n            example.ParseFromString(raw_record.numpy())\n            if example.features.feature['StudyInstanceUID'].bytes_list.value[0].decode('utf-8') == image_id:\n                return tf.image.decode_png(example.features.feature['image'].bytes_list.value[0])  # Decode image\n    return None  # If image not found\n\n# Example usage\ntfrecord_files = [f'/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/{i:02d}-1881.tfrec' for i in range(16)]\ndf = extract_features_from_tfrecords(tfrecord_files)\n\n# Get the first 1000 IDs\nmerged_ids = merged_df['StudyInstanceUID'].head(1).tolist()\n\n# Match IDs\nmatched_ids = set(merged_ids) & set(df['StudyInstanceUID'].tolist())\n\n# List to store images\nimages = []\n\n# For each matched ID, read the image directly from TFRecord\nfor image_id in matched_ids:\n    image = load_image_from_tfrecord(image_id, tfrecord_files)\n    if image is not None:\n        images.append(image)\n\n# View the number and size of images\nprint(f'Number of images: {len(images)}')\nif images:\n    print(f'Shape of the first image: {images[0].shape}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T07:54:30.797279Z","iopub.execute_input":"2025-05-14T07:54:30.797868Z","iopub.status.idle":"2025-05-14T07:54:58.589103Z","shell.execute_reply.started":"2025-05-14T07:54:30.797844Z","shell.execute_reply":"2025-05-14T07:54:58.588373Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd\nimport time\n\ndef extract_features_from_tfrecords(tfrecord_files):\n    all_data = []\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n        \n        def _parse_function(proto):\n            feature_description = {\n                'StudyInstanceUID': tf.io.FixedLenFeature([], tf.string, default_value=''),\n                'CVC - Abnormal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'CVC - Borderline': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'CVC - Normal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'ETT - Abnormal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'ETT - Borderline': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'ETT - Normal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Abnormal': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Borderline': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Incompletely Imaged': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'NGT - Normal': tf.io.FixedLenFeature([], tf.int64, default_value=0),        \n                'Swan Ganz Catheter Present': tf.io.FixedLenFeature([], tf.int64, default_value=0),\n                'image': tf.io.FixedLenFeature([], tf.string),  # Add image feature\n            }\n            return tf.io.parse_single_example(proto, feature_description)\n\n        parsed_dataset = raw_dataset.map(_parse_function)\n        \n        for parsed_record in parsed_dataset:\n            record_data = {key: value.numpy() for key, value in parsed_record.items()}\n            record_data['StudyInstanceUID'] = record_data['StudyInstanceUID'].decode('utf-8')\n            all_data.append(record_data)\n\n    df = pd.DataFrame(all_data)\n    cols = ['StudyInstanceUID'] + [col for col in df.columns if col != 'StudyInstanceUID']\n    df = df[cols]\n    return df\n\n# Function to read images, extracting image data from TFRecord\ndef load_image_from_tfrecord(image_id, tfrecord_files):\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n        for raw_record in raw_dataset:\n            example = tf.train.Example()\n            example.ParseFromString(raw_record.numpy())\n            if example.features.feature['StudyInstanceUID'].bytes_list.value[0].decode('utf-8') == image_id:\n                return tf.image.decode_png(example.features.feature['image'].bytes_list.value[0])  # Decode image\n    return None  # If image not found\n\n# Example usage\ntfrecord_files = [f'/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/{i:02d}-1881.tfrec' for i in range(16)]\ndf = extract_features_from_tfrecords(tfrecord_files)\n\n# Get the first 1000 IDs\nmerged_ids = merged_df['StudyInstanceUID'].head(1000).tolist()\n\n# Match IDs\nmatched_ids = set(merged_ids) & set(df['StudyInstanceUID'].tolist())\n\n# List to store images\nimages = []\n\n# Start timing\nstart_time = time.time()\n\n# For each matched ID, read the image directly from TFRecord\nfor index, image_id in enumerate(matched_ids):\n    image = load_image_from_tfrecord(image_id, tfrecord_files)\n    if image is not None:\n        images.append(image)\n    \n    # Print time taken to extract every 1 image\n    if (index + 1) % 1 == 0:\n        elapsed_time = time.time() - start_time\n        print(f'Time taken to extract {index + 1} images: {elapsed_time:.4f} seconds')\n\n# End timing\nend_time = time.time()\n\n# View the number and size of images\nprint(f'Number of images: {len(images)}')\nif images:\n    print(f'Shape of the first image: {images[0].shape}')\n\n# Print total extraction time\nprint(f'Total time taken to extract images: {end_time - start_time:.4f} seconds')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T07:57:56.726531Z","iopub.execute_input":"2025-05-14T07:57:56.726809Z","iopub.status.idle":"2025-05-14T08:00:01.640532Z","shell.execute_reply.started":"2025-05-14T07:57:56.726789Z","shell.execute_reply":"2025-05-14T08:00:01.639403Z"},"jupyter":{"outputs_hidden":true,"source_hidden":true},"collapsed":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Overall export 1\nimport tensorflow as tf\nimport pandas as pd\nimport os\nimport time\n\n# Create root directory to save images\noutput_dir = '/kaggle/working/images'  # This might also help speed things up, reducing GPU usage.\nos.makedirs(output_dir, exist_ok=True)\n\ndef extract_features_from_tfrecords(tfrecord_files):\n    all_data = []\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n        \n        def _parse_function(proto):\n            feature_description = {  # Previously included many features, now only extracting id and image\n                'StudyInstanceUID': tf.io.FixedLenFeature([], tf.string, default_value=''),\n                'image': tf.io.FixedLenFeature([], tf.string),\n            }\n            return tf.io.parse_single_example(proto, feature_description)\n\n        parsed_dataset = raw_dataset.map(_parse_function)\n        for parsed_record in parsed_dataset:\n            record_data = {key: value.numpy() for key, value in parsed_record.items()}\n            record_data['StudyInstanceUID'] = record_data['StudyInstanceUID'].decode('utf-8')\n            all_data.append(record_data)\n\n    df = pd.DataFrame(all_data)\n    return df\n\n# Read images and save them to the specified directory by category\ndef save_images_to_directory(image_ids, tfrecord_files):\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n        for raw_record in raw_dataset:\n            example = tf.train.Example()\n            example.ParseFromString(raw_record.numpy())\n\n            # This part is newly added.\n            uid = example.features.feature['StudyInstanceUID'].bytes_list.value[0].decode('utf-8')\n            if uid in image_ids:\n                image_data = example.features.feature['image'].bytes_list.value[0]\n                image = tf.image.decode_png(image_data)\n                \n                # Create subfolders by category (e.g., based on the first few characters of UID)\n                category = uid.split('.')[2]  # Change classification logic as needed\n                category_dir = os.path.join(output_dir, category)\n                os.makedirs(category_dir, exist_ok=True)\n                \n                image_path = os.path.join(category_dir, f\"{uid}.png\")\n                tf.io.write_file(image_path, tf.image.encode_png(image))\n\n# Example usage\ntfrecord_files = [f'/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/{i:02d}-1881.tfrec' for i in range(16)]\ndf = extract_features_from_tfrecords(tfrecord_files)\n\n# Get the first 1000 IDs\nmerged_ids = merged_df['StudyInstanceUID'].head(1000).tolist()\nmatched_ids = set(merged_ids) & set(df['StudyInstanceUID'].tolist())\n\n# Start timing\nstart_time = time.time()\n\n# Save images to the specified directory\nsave_images_to_directory(matched_ids, tfrecord_files)\n\n# End timing\nend_time = time.time()\nprint(f'Total time taken to save images: {end_time - start_time:.4f} seconds')\n\n# Now you can directly use the saved images for training","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T08:05:39.019655Z","iopub.execute_input":"2025-05-14T08:05:39.019958Z","iopub.status.idle":"2025-05-14T08:09:09.740574Z","shell.execute_reply.started":"2025-05-14T08:05:39.019935Z","shell.execute_reply":"2025-05-14T08:09:09.73991Z"},"collapsed":true,"jupyter":{"outputs_hidden":true,"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Overall export 2 -- This version had classification based only on UID, which is meaningless, so it has been removed.\nimport tensorflow as tf\nimport pandas as pd\nimport os\nimport time\n\n# Create root directory to save images\noutput_dir = '/kaggle/working/images'\nos.makedirs(output_dir, exist_ok=True)\n\ndef extract_features_from_tfrecords(tfrecord_files):\n    all_data = []\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n        \n        def _parse_function(proto):\n            feature_description = {\n                'StudyInstanceUID': tf.io.FixedLenFeature([], tf.string, default_value=''),\n                'image': tf.io.FixedLenFeature([], tf.string),\n            }\n            return tf.io.parse_single_example(proto, feature_description)\n\n        parsed_dataset = raw_dataset.map(_parse_function)\n        for parsed_record in parsed_dataset:\n            record_data = {key: value.numpy() for key, value in parsed_record.items()}\n            record_data['StudyInstanceUID'] = record_data['StudyInstanceUID'].decode('utf-8')\n            all_data.append(record_data)\n\n    df = pd.DataFrame(all_data)\n    return df\n\n# Read images and save them to the specified directory\ndef save_images_to_directory(image_ids, tfrecord_files):\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n        for raw_record in raw_dataset:\n            example = tf.train.Example()\n            example.ParseFromString(raw_record.numpy())\n\n            uid = example.features.feature['StudyInstanceUID'].bytes_list.value[0].decode('utf-8')\n            if uid in image_ids:\n                image_data = example.features.feature['image'].bytes_list.value[0]\n                image = tf.image.decode_png(image_data)\n                \n                # Save directly to a unified directory\n                image_path = os.path.join(output_dir, f\"{uid}.png\")\n                tf.io.write_file(image_path, tf.image.encode_png(image))\n\n# Example usage\ntfrecord_files = [f'/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/{i:02d}-1881.tfrec' for i in range(16)]\ndf = extract_features_from_tfrecords(tfrecord_files)\n\n# Get the first 1000 IDs\nmerged_ids = merged_df['StudyInstanceUID'].head(1000).tolist()\nmatched_ids = set(merged_ids) & set(df['StudyInstanceUID'].tolist())\n\n# Start timing\nstart_time = time.time()\n\n# Save images to the specified directory\nsave_images_to_directory(matched_ids, tfrecord_files)\n\n# End timing\nend_time = time.time()\nprint(f'Total time taken to save images: {end_time - start_time:.4f} seconds')\n\n# Now you can directly use the saved images for training","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T03:34:25.315227Z","iopub.execute_input":"2025-05-15T03:34:25.3155Z","iopub.status.idle":"2025-05-15T03:37:53.571189Z","shell.execute_reply.started":"2025-05-15T03:34:25.31548Z","shell.execute_reply":"2025-05-15T03:37:53.570522Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport tensorflow as tf\nimport pandas as pd\nimport time\n\n# Create root directory to save images\noutput_dir = '/kaggle/working/images'\nos.makedirs(output_dir, exist_ok=True)\n\ndef extract_features_from_tfrecords(tfrecord_files):\n    all_data = []\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n        \n        def _parse_function(proto):\n            feature_description = {\n                'StudyInstanceUID': tf.io.FixedLenFeature([], tf.string, default_value=''),\n                'image': tf.io.FixedLenFeature([], tf.string),\n            }\n            return tf.io.parse_single_example(proto, feature_description)\n\n        parsed_dataset = raw_dataset.map(_parse_function)\n        for parsed_record in parsed_dataset:\n            record_data = {key: value.numpy() for key, value in parsed_record.items()}\n            record_data['StudyInstanceUID'] = record_data['StudyInstanceUID'].decode('utf-8')\n            all_data.append(record_data)\n\n    df = pd.DataFrame(all_data)\n    return df\n\ndef save_images_to_directory(image_ids, tfrecord_files):\n    saved_count = 0\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n        for raw_record in raw_dataset:\n            example = tf.train.Example()\n            example.ParseFromString(raw_record.numpy())\n\n            uid = example.features.feature['StudyInstanceUID'].bytes_list.value[0].decode('utf-8')\n            if uid in image_ids:\n                image_data = example.features.feature['image'].bytes_list.value[0]\n                image = tf.image.decode_png(image_data)\n\n                if image is not None:\n                    image_path = os.path.join(output_dir, f\"{uid}.png\")\n                    tf.io.write_file(image_path, tf.image.encode_png(image))\n                    saved_count += 1\n\n    print(f\"Successfully saved number of images: {saved_count}\")\n\ndef count_files_and_images(path, matched_ids):\n    total_files = 0\n    image_files = 0\n    unmatched_ids = []\n    image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff'}\n\n    for root, dirs, files in os.walk(path):\n        for file in files:\n            total_files += 1\n            uid = os.path.splitext(file)[0]\n\n            if os.path.splitext(file)[1].lower() in image_extensions:\n                image_files += 1\n            else:\n                if uid not in matched_ids:\n                    unmatched_ids.append(uid)\n\n            if uid not in matched_ids and os.path.splitext(file)[1].lower() in image_extensions:\n                unmatched_ids.append(uid)\n\n    return total_files, image_files, unmatched_ids\n\n# Example usage\ntfrecord_files = [f'/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/{i:02d}-1881.tfrec' for i in range(16)]\ndf = extract_features_from_tfrecords(tfrecord_files)\n\n# Get the first 1000 UIDs\nmerged_ids = merged_df['StudyInstanceUID'].head(1000).tolist()\nmatched_ids = set(merged_ids) & set(df['StudyInstanceUID'].tolist())\n\n# Start timing\nstart_time = time.time()\n\n# Save images to the specified directory\nsave_images_to_directory(matched_ids, tfrecord_files)\n\n# End timing\nend_time = time.time()\nprint(f'Total time taken to save images: {end_time - start_time:.4f} seconds')\n\n# Count files and images\ntotal_files, image_files, unmatched_ids = count_files_and_images(output_dir, matched_ids)\n\nprint(f\"Total number of files: {total_files}, Number of image files: {image_files}\")\nprint(f\"Unmatched UID filenames: {unmatched_ids}\")\nprint(f\"Remaining unmatched count: {len(unmatched_ids)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T04:49:17.150471Z","iopub.execute_input":"2025-05-15T04:49:17.151131Z","iopub.status.idle":"2025-05-15T04:52:45.270065Z","shell.execute_reply.started":"2025-05-15T04:49:17.151105Z","shell.execute_reply":"2025-05-15T04:52:45.269194Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Investigating why the quantity is low --- 1\nimport os\n\ndef count_files_and_images(path):\n    total_files = 0\n    image_files = 0\n    image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff'}\n\n    for root, dirs, files in os.walk(path):\n        for file in files:\n            total_files += 1\n            if os.path.splitext(file)[1].lower() in image_extensions:\n                image_files += 1\n\n    return total_files, image_files\n\n# Example usage\npath = '/kaggle/working/images'  # Replace with the path you want to check\ntotal_files, image_files = count_files_and_images(path)\nprint(f\"Total number of files: {total_files}, Number of image files: {image_files}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T03:38:07.785302Z","iopub.execute_input":"2025-05-15T03:38:07.785808Z","iopub.status.idle":"2025-05-15T03:38:07.792935Z","shell.execute_reply.started":"2025-05-15T03:38:07.785784Z","shell.execute_reply":"2025-05-15T03:38:07.792291Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Investigating why the quantity is low --- 2. Try to output unmatched items\n\nimport os\n\ndef count_files_and_images(path, matched_ids):\n    total_files = 0\n    image_files = 0\n    unmatched_ids = []\n    image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff'}\n\n    for root, dirs, files in os.walk(path):\n        for file in files:\n            total_files += 1\n            if os.path.splitext(file)[1].lower() in image_extensions:\n                image_files += 1\n            else:\n                # Collect unmatched UIDs\n                uid = os.path.splitext(file)[0]  # Get the filename without the extension\n                if uid not in matched_ids:\n                    unmatched_ids.append(uid)\n\n    return total_files, image_files, unmatched_ids\n\n# Example usage\npath = '/kaggle/working/images'  # Replace with the path you want to check\n# Assume matched_ids is the list of UIDs you obtained earlier\nmatched_ids = set(matched_ids)  # Ensure matched_ids is a set for faster lookup\n\ntotal_files, image_files, unmatched_ids = count_files_and_images(path, matched_ids)\n\nprint(f\"Total number of files: {total_files}, Number of image files: {image_files}\")\nprint(f\"Unmatched UID filenames: {unmatched_ids}\")\nprint(f\"Remaining unmatched count: {len(unmatched_ids)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T03:44:31.125166Z","iopub.execute_input":"2025-05-15T03:44:31.125428Z","iopub.status.idle":"2025-05-15T03:44:31.133763Z","shell.execute_reply.started":"2025-05-15T03:44:31.125407Z","shell.execute_reply":"2025-05-15T03:44:31.133226Z"},"collapsed":true,"jupyter":{"outputs_hidden":true,"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\ndef count_files_and_images(path, matched_ids):\n    total_files = 0\n    image_files = 0\n    unmatched_ids = []\n    image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff'}\n\n    for root, dirs, files in os.walk(path):\n        for file in files:\n            total_files += 1\n            uid = os.path.splitext(file)[0]  # Get the filename without the extension\n\n            if os.path.splitext(file)[1].lower() in image_extensions:\n                image_files += 1\n            else:\n                # Originally, the logic here was to collect unmatched UIDs\n                if uid not in matched_ids:\n                    unmatched_ids.append(uid)\n\n            # Also check if the UID of image files matches\n            if uid not in matched_ids and os.path.splitext(file)[1].lower() in image_extensions:\n                unmatched_ids.append(uid)\n\n    return total_files, image_files, unmatched_ids\n\n# Example usage\npath = '/kaggle/working/images'  # Replace with the path you want to check\nmatched_ids = set(matched_ids)  # Ensure matched_ids is a set for faster lookup\n\ntotal_files, image_files, unmatched_ids = count_files_and_images(path, matched_ids)\n\nprint(f\"Total number of files: {total_files}, Number of image files: {image_files}\")\nprint(f\"Unmatched UID filenames: {unmatched_ids}\")\nprint(f\"Remaining unmatched count: {len(unmatched_ids)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T03:46:51.53287Z","iopub.execute_input":"2025-05-15T03:46:51.533445Z","iopub.status.idle":"2025-05-15T03:46:51.542773Z","shell.execute_reply.started":"2025-05-15T03:46:51.533421Z","shell.execute_reply":"2025-05-15T03:46:51.542196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Count the frequency of UID occurrence\nuid_counts = merged_df['StudyInstanceUID'].value_counts()\n\n# Find duplicate Uids\nduplicate_uids = uid_counts[uid_counts > 1]\n\n# Print the occurrence frequency of all Uids\nprint(\" The occurrence frequency of each UID :\")\nprint(uid_counts)\n\n# Print duplicate Uids\nprint(\" Duplicate UID:\")\nprint(duplicate_uids)\n\n# Output the number of duplicate Uids\nprint(f\"\\ nThe number of duplicate Uids: {len(duplicate_uids)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T04:16:22.073225Z","iopub.execute_input":"2025-05-15T04:16:22.073772Z","iopub.status.idle":"2025-05-15T04:16:22.089211Z","shell.execute_reply.started":"2025-05-15T04:16:22.073752Z","shell.execute_reply":"2025-05-15T04:16:22.088483Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"It was only here that I realized that because I initially integrated two csv files, there were many duplicate anchor boxes","metadata":{}},{"cell_type":"code","source":"tfmerged_df = merged_df.merge(matched_df, on='StudyInstanceUID', how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T04:24:04.379079Z","iopub.execute_input":"2025-05-15T04:24:04.379664Z","iopub.status.idle":"2025-05-15T04:24:04.394399Z","shell.execute_reply.started":"2025-05-15T04:24:04.379638Z","shell.execute_reply":"2025-05-15T04:24:04.393841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Suppose images[0] is the image you want to display\nplt.imshow(images[0].numpy()) # Convert to a NumPy array\nplt.axis('off') # Does not display the coordinate axes\nplt.show() # Display images","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T07:55:37.69354Z","iopub.execute_input":"2025-05-14T07:55:37.69388Z","iopub.status.idle":"2025-05-14T07:55:37.863433Z","shell.execute_reply.started":"2025-05-14T07:55:37.693846Z","shell.execute_reply":"2025-05-14T07:55:37.862697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\n\ntfrecord_files = [f'/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/{i:02d}-1881.tfrec' for i in range(16)]\n\ndef extract_images_and_labels_from_tfrecords(tfrecord_files, merged_df, max_samples=1000):\n    images_dict = {}\n    labels = []\n    \n    # Get the first max_samples rows of StudyInstanceUID and labels\n    relevant_df = merged_df.dropna(subset=['label']).head(max_samples)\n    uids = relevant_df['StudyInstanceUID'].values\n\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n\n        for raw_record in raw_dataset:\n            if len(images_dict) >= max_samples:  # Check if the maximum sample count has been reached\n                break\n            \n            example = tf.train.Example()\n            example.ParseFromString(raw_record.numpy())\n            \n            # Extract image UID\n            uid = example.features.feature['StudyInstanceUID'].bytes_list.value[0].decode('utf-8')\n\n            # Only process records matching UIDs in relevant_df\n            if uid in uids:\n                # Extract image\n                image_data = example.features.feature['image'].bytes_list.value[0]\n                image = tf.image.decode_png(image_data)\n                image = tf.image.resize(image, (224, 224))  # Resize\n                image = image.numpy() / 255.0  # Normalize\n                image = image.reshape((224, 224, 1))  # Change shape to (224, 224, 1)\n\n                # Store image in dictionary\n                if uid not in images_dict:\n                    images_dict[uid] = image\n\n                # Extract labels\n                label = [\n                    example.features.feature['CVC - Abnormal'].int64_list.value[0],\n                    example.features.feature['CVC - Borderline'].int64_list.value[0],\n                    example.features.feature['CVC - Normal'].int64_list.value[0],\n                    example.features.feature['ETT - Abnormal'].int64_list.value[0],\n                    example.features.feature['ETT - Borderline'].int64_list.value[0],\n                    example.features.feature['ETT - Normal'].int64_list.value[0],\n                    example.features.feature['NGT - Abnormal'].int64_list.value[0],\n                    example.features.feature['NGT - Borderline'].int64_list.value[0],\n                    example.features.feature['NGT - Incompletely Imaged'].int64_list.value[0],\n                    example.features.feature['NGT - Normal'].int64_list.value[0],\n                    example.features.feature['Swan Ganz Catheter Present'].int64_list.value[0]\n                ]\n                labels.append(label)\n\n    # Create final image array\n    final_images = np.zeros((len(uids), 224, 224, 1))  # Initialize array\n    for i, uid in enumerate(uids):\n        if uid in images_dict:\n            final_images[i] = images_dict[uid]\n        else:\n            final_images[i] = np.zeros((224, 224, 1))  # Fill with zeros if no image\n\n    return final_images, np.array(labels)\n\n# Example usage\ntfrecord_images, tfrecord_labels = extract_images_and_labels_from_tfrecords(tfrecord_files, merged_df, max_samples=1000)\nprint(tfrecord_images.shape)  # Should be (1000, 224, 224, 1)\nprint(tfrecord_labels.shape)  # Check label shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T06:44:11.47151Z","iopub.execute_input":"2025-05-15T06:44:11.471855Z","iopub.status.idle":"2025-05-15T06:44:36.321501Z","shell.execute_reply.started":"2025-05-15T06:44:11.471834Z","shell.execute_reply":"2025-05-15T06:44:36.320791Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models\n\n# Define image input\nimage_input = layers.Input(shape=(224, 224, 3), name='image_input')\nimage_features = layers.Conv2D(32, (3, 3), activation='relu')(image_input)\nimage_features = layers.MaxPooling2D()(image_features)\nimage_features = layers.Flatten()(image_features)\n\n# Define the image input extracted from TFRecord\ntfrecord_image_input = layers.Input(shape=(224, 224, 1), name='tfrecord_image_input')\ntfrecord_image_features = layers.Conv2D(32, (3, 3), activation='relu')(tfrecord_image_input)\ntfrecord_image_features = layers.MaxPooling2D()(tfrecord_image_features)\ntfrecord_image_features = layers.Flatten()(tfrecord_image_features)\n\n# Numerical features extracted from CSV\nnum_csv_features = 11 # Adjust according to the number of your tags or features\ncsv_input = layers.Input(shape=(num_csv_features,), name='csv_input')\n\n# Merge all features\nmerged = layers.Concatenate()([image_features, tfrecord_image_features, csv_input])\n\n# Continue to build the model\ndense = layers.Dense(64, activation='relu')(merged)\noutput = layers.Dense(num_csv_features, activation='softmax')(dense) # Adjust according to the number of labels\n\n# Create a Model\nmodel = models.Model(inputs=[image_input, tfrecord_image_input, csv_input], outputs=output)\n\n# Compilation Model\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\nWhen training the model, ensure that the input data format is correct\nhistory = model.fit(\n    [input_images, tfrecord_images, input_features], # Input data\n    labels, # Labels\n    epochs=2,\n    batch_size=2\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T06:18:34.480619Z","iopub.execute_input":"2025-05-15T06:18:34.481339Z","iopub.status.idle":"2025-05-15T06:18:35.976335Z","shell.execute_reply.started":"2025-05-15T06:18:34.481306Z","shell.execute_reply":"2025-05-15T06:18:35.975308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Input Images Shape:\", input_images.shape) # Check the shape of the image input\nprint(\"TFRecord Images Shape:\", tfrecord_images.shape) # Check the shape of the image extracted from TFRecord\nprint(\"Input Features Shape:\", input_features.shape) # Check the shape of CSV features\nprint(\"Labels Shape:\", labels.shape) # Check the shape of the label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T06:30:02.292841Z","iopub.execute_input":"2025-05-15T06:30:02.293122Z","iopub.status.idle":"2025-05-15T06:30:02.297781Z","shell.execute_reply.started":"2025-05-15T06:30:02.293102Z","shell.execute_reply":"2025-05-15T06:30:02.297039Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Remove the last dimension\nif len(tfrecord_images.shape) == 5:\ntfrecord_images = np.squeeze(tfrecord_images, axis=-1) # Remove the last dimension\n\n# Check the adjusted shape\nprint(\"Adjusted TFRecord Images Shape:\", tfrecord_images.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T05:14:24.07965Z","iopub.execute_input":"2025-05-15T05:14:24.08017Z","iopub.status.idle":"2025-05-15T05:14:24.084829Z","shell.execute_reply.started":"2025-05-15T05:14:24.080144Z","shell.execute_reply":"2025-05-15T05:14:24.084115Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define image input\nimage_input = layers.Input(shape=(224, 224, 3), name='image_input')\n# Define the TFRecord image input\ntfrecord_image_input = layers.Input(shape=(224, 224, 3), name='tfrecord_image_input')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T05:15:02.53073Z","iopub.execute_input":"2025-05-15T05:15:02.531146Z","iopub.status.idle":"2025-05-15T05:15:02.536565Z","shell.execute_reply.started":"2025-05-15T05:15:02.531123Z","shell.execute_reply":"2025-05-15T05:15:02.535762Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models\n\n# Define image input\nimage_input = layers.Input(shape=(224, 224, 3), name='image_input')\nimage_features = layers.Conv2D(32, (3, 3), activation='relu')(image_input)\nimage_features = layers.MaxPooling2D()(image_features)\nimage_features = layers.Flatten()(image_features)\n\n# Define the TFRecord image input\ntfrecord_image_input = layers.Input(shape=(224, 224, 3), name='tfrecord_image_input')\ntfrecord_image_features = layers.Conv2D(32, (3, 3), activation='relu')(tfrecord_image_input)\ntfrecord_image_features = layers.MaxPooling2D()(tfrecord_image_features)\ntfrecord_image_features = layers.Flatten()(tfrecord_image_features)\n\n# Numerical features extracted from CSV\n# num_csv_features = 11\ncsv_input = layers.Input(shape=(num_csv_features,), name='csv_input')\n\n# Merge all features\nmerged = layers.Concatenate()([image_features, tfrecord_image_features, csv_input]) ''''''\n\n# Continue to build the model\ndense = layers.Dense(64, activation='relu')(merged)\n# output = layers.Dense(num_csv_features, activation='softmax')(dense) # Adjust according to the number of labels\noutput = layers.Dense(11, activation='softmax')(dense) # num_classes should be the actual number of classes\n\n# Create a Model\nmodel = models.Model(inputs=[image_input, tfrecord_image_input, csv_input], outputs=output)  ''''''\n\n# Compilation Model\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T06:28:09.476215Z","iopub.execute_input":"2025-05-15T06:28:09.476466Z","iopub.status.idle":"2025-05-15T06:28:09.546886Z","shell.execute_reply.started":"2025-05-15T06:28:09.476449Z","shell.execute_reply":"2025-05-15T06:28:09.546303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(input_images), len(tfrecord_images), len(input_features), len(labels))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T06:29:33.661071Z","iopub.execute_input":"2025-05-15T06:29:33.66134Z","iopub.status.idle":"2025-05-15T06:29:33.665719Z","shell.execute_reply.started":"2025-05-15T06:29:33.661322Z","shell.execute_reply":"2025-05-15T06:29:33.665073Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the Model\nhistory = model.fit(\n    [image_array, tfrecord_images, input_features], # Input data\n    labels, # Labels\n    epochs=2,\n    batch_size=2\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T06:37:54.786508Z","iopub.execute_input":"2025-05-15T06:37:54.78708Z","iopub.status.idle":"2025-05-15T06:37:54.805176Z","shell.execute_reply.started":"2025-05-15T06:37:54.787058Z","shell.execute_reply":"2025-05-15T06:37:54.804246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# Load the CSV file\ntrain_df = pd.read_csv('/kaggle/input/ranzcr-clip-catheter-line-classification/train.csv')\nannotations_df = pd.read_csv('/kaggle/input/ranzcr-clip-catheter-line-classification/train_annotations.csv')\n\n# View the column names of annotations_df\nprint(annotations_df.columns)\n\n# Merge data frames\nmerged_df = train_df.merge(annotations_df, on='StudyInstanceUID', how='left')\n\n# Extract Features\nfeatures = merged_df[['ETT - Abnormal', 'ETT - Borderline',\n    'ETT - Normal', 'NGT - Abnormal', 'NGT - Borderline',\n    'NGT - Incompletely Imaged', 'NGT - Normal',\n    'CVC - Abnormal', 'CVC - Borderline',\n    'CVC - Normal', 'Swan Ganz Catheter Present',\n    'PatientID']].values\n\n# Extract Tags\nlabels = merged_df['label'].values # There should be no problem here\n\n# View the merged data\nprint(merged_df.head())\n\nmerged_df = merged_df.dropna(subset=['label']) # Delete lines containing NaN\n\n\nprint(merged_df['label'].head()) # View the first few lines of labels\nprint(merged_df['label'].unique()) # View the unique value\n\n\n\nfrom sklearn.preprocessing import LabelEncoder\n\nlabel_encoder = LabelEncoder()\nlabels = label_encoder.fit_transform(merged_df['label'].values)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T06:37:41.846017Z","iopub.execute_input":"2025-05-15T06:37:41.846511Z","iopub.status.idle":"2025-05-15T06:37:41.877727Z","shell.execute_reply.started":"2025-05-15T06:37:41.846491Z","shell.execute_reply":"2025-05-15T06:37:41.877052Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1. Load and process the first 1000 JPEG files\nfrom PIL import Image\nimport numpy as np\n\n# Suppose merged_df is your DataFrame\nnum_samples = min(1000, len(merged_df)) # Take the total number of samples of the first 1000 or dataframes\nimage_array = np.zeros((num_samples, 224, 224, 3)) # Initialize the array\n\n# Load each image\nfor i in range(num_samples):\n    study_instance_uid = merged_df['StudyInstanceUID'].iloc[i] # Get the current StudyInstanceUID\n    image_path = f'/kaggle/input/ranzcr-clip-catheter-line classification/train/{study_instance_uid}.jpg' # Build the path\n    \n    # Load JPEG images\n    Image = image.open (image_path).convert('RGB') # Convert to RGB\n    image = Image.resize ((224, 224)) # resize\n    image_array[i] = np.array(image) / 255.0 # Normalize and store in the array\n\n# Now the image_array contains the first 1000 images\nprint(image_array.shape) # should be (num_samples, 224, 224, 3)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport os\n\n# Define the path to TFRecord files\ntfrecord_files = [f'/kaggle/input/ranzcr-clip-catheter-line-classification/train_tfrecords/{i:02d}-1881.tfrec' for i in range(16)]\noutput_dir = '/kaggle/working/images/'  # Output path\n\n# Create output directory (if it does not exist)\nos.makedirs(output_dir, exist_ok=True)\n\ndef extract_and_save_all_images(tfrecord_files):\n    for tfrecord_file in tfrecord_files:\n        raw_dataset = tf.data.TFRecordDataset(tfrecord_file)\n\n        for raw_record in raw_dataset:\n            example = tf.train.Example()\n            example.ParseFromString(raw_record.numpy())\n            \n            # Extract image UID\n            uid = example.features.feature['StudyInstanceUID'].bytes_list.value[0].decode('utf-8')\n\n            # Extract image\n            image_data = example.features.feature['image'].bytes_list.value[0]\n            image = tf.image.decode_png(image_data)\n            image = tf.image.resize(image, (224, 224))  # Resize\n            image = image.numpy() / 255.0  # Normalize\n\n            # Save image as JPEG file\n            image_path = os.path.join(output_dir, f'{uid}.jpg')\n            tf.keras.preprocessing.image.save_img(image_path, image)\n\n    print(f\"All images extracted and saved to {output_dir}\")\n\n# Example usage\nextract_and_save_all_images(tfrecord_files)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T06:51:57.179866Z","iopub.execute_input":"2025-05-15T06:51:57.180586Z","iopub.status.idle":"2025-05-15T06:55:13.76315Z","shell.execute_reply.started":"2025-05-15T06:51:57.180567Z","shell.execute_reply":"2025-05-15T06:55:13.762391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\n\n# Suppose merged_df is your DataFrame\nnum_samples_tf = min(1000, len(merged_df)) # Take the total number of samples of the first 1000 or dataframes\nimage_tf_array = np.zeros((num_samples_tf, 224, 224, 3)) # Initialize the array\n\n# Load each image\nfor i in range(num_samples):\n    study_instance_uid_tf = merged_df['StudyInstanceUID'].iloc[i] # Get the current StudyInstanceUID\n    image_tf_path = f'/kaggle/working/images/{study_instance_uid}.jpg' # Build the path\n    \n    # Load JPEG images using TensorFlow\n    image_tf = tf.io.read_file(image_tf_path) # Read the file\n    image_tf = tf.image.decode_jpeg(image_tf, channels=3) # decoded as an RGB image\n    image_tf = tf.image.resize(image_tf, (224, 224)) # Resize\n    image_tf_array[i] = image_tf.numpy() / 255.0 # Normalize and store in the array\n\n# Now the image_array contains the first 1000 images\nprint(image_tf_array.shape) # should be (num_samples, 224, 224, 3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T07:00:57.691081Z","iopub.execute_input":"2025-05-15T07:00:57.691654Z","iopub.status.idle":"2025-05-15T07:01:01.009998Z","shell.execute_reply.started":"2025-05-15T07:00:57.691632Z","shell.execute_reply":"2025-05-15T07:01:01.009082Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models, optimizers\n\n# Assume you already have raw data:\n# merged_df: DataFrame containing 10 numeric columns + StudyInstanceUID/PatientID/label\n# image_array:      list or np.array, shape=(N, 224, 224, 3)\n# image_tf_array:   list or np.array, shape=(N, 224, 224, 3)\n# labels:           list or np.array, shape=(N,) (string or int)\n\n# 0) Standardize conversion to NumPy array and enforce data type\nimage_array    = np.array(image_array,    dtype=np.float32)\nimage_tf_array = np.array(image_tf_array, dtype=np.float32)\n\n# Explicitly specify feature columns\nfeature_cols = [\n    'ETT - Abnormal', 'ETT - Borderline', 'ETT - Normal',\n    'NGT - Abnormal', 'NGT - Borderline', 'NGT - Incompletely Imaged', 'NGT - Normal',\n    'CVC - Abnormal', 'CVC - Borderline', 'CVC - Normal',\n    'Swan Ganz Catheter Present'\n]\nfeatures = merged_df[feature_cols].values.astype(np.float32)\n\n# Convert labels to integers\nlabels = np.array(labels)\nif labels.dtype.type is np.str_ or labels.dtype == object:\n    from sklearn.preprocessing import LabelEncoder\n    le = LabelEncoder()\n    labels = le.fit_transform(labels)\nlabels = labels.astype(np.int32)\n\n# Check dtype\nprint('dtypes:', image_array.dtype, image_tf_array.dtype, features.dtype, labels.dtype)\n\n# Input dimensions\nnum_tab = features.shape[1]\nnum_cls = len(np.unique(labels))\n\n# 1) Define three input branches\nimg1_input = layers.Input(shape=(224, 224, 3), name='img1_input')\nimg2_input = layers.Input(shape=(224, 224, 3), name='img2_input')\ntab_input  = layers.Input(shape=(num_tab,),   name='tab_input')\n\n# 2) Image branch A: ResNet50\ntry:\n    base_a = tf.keras.applications.ResNet50(weights='imagenet', include_top=False, input_tensor=img1_input)\nexcept Exception:\n    base_a = tf.keras.applications.ResNet50(weights=None, include_top=False, input_tensor=img1_input)\nxa = layers.GlobalAveragePooling2D()(base_a.output)\nxa = layers.Dense(128, activation='relu')(xa)\n\n# 3) Image branch B: EfficientNetB0\ntry:\n    base_b = tf.keras.applications.EfficientNetB0(weights='imagenet', include_top=False, input_tensor=img2_input)\nexcept Exception:\n    base_b = tf.keras.applications.EfficientNetB0(weights=None, include_top=False, input_tensor=img2_input)\nxb = layers.GlobalAveragePooling2D()(base_b.output)\nxb = layers.Dense(128, activation='relu')(xb)\n\n# 4) Tabular branch\nxt = layers.Dense(64, activation='relu')(tab_input)\nxt = layers.Dense(32, activation='relu')(xt)\n\n# 5) Intermediate fusion\nmerged = layers.concatenate([xa, xb, xt])\n\n# 6) Post-fusion layers\nx = layers.Dense(64, activation='relu')(merged)\nx = layers.Dropout(0.5)(x)\nout = layers.Dense(num_cls, activation='softmax')(x)\n\n# 7) Build and compile the model\nmodel = models.Model([img1_input, img2_input, tab_input], out)\nmodel.compile(optimizer=optimizers.Adam(1e-4), loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n# model.summary()\n\n# 8) Training\nhistory = model.fit(\n    x={'img1_input': image_array, 'img2_input': image_tf_array, 'tab_input': features},\n    y=labels,\n    batch_size=32, epochs=15,\n    validation_split=0.2, shuffle=True\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T07:14:07.431659Z","iopub.execute_input":"2025-05-15T07:14:07.432378Z","iopub.status.idle":"2025-05-15T07:19:04.81668Z","shell.execute_reply.started":"2025-05-15T07:14:07.432351Z","shell.execute_reply":"2025-05-15T07:19:04.815894Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}