Powered by Cognitive K.i.
Example code of a basic Convolutional Neural Network (CNN
​
Example code of basic Convolutional Neural Network (CNN) model in Python, specifically designed for medical imaging in oncology research:
```python
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models
# Load the dataset and preprocess it
# Assuming you have X_train, y_train, X_test, y_test as your dataset
# Normalize the images
X_train = X_train.astype('float32') / 255 X_test = X_test.astype('float32') / 255
# One-hot encode the labels
num_classes = np.max(y_train) + 1
y_train = tf.keras.utils.to_categorical(y_train, num_classes) y_test = tf.keras.utils.to_categorical(y_test, num_classes)
# Build the CNN model
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(img_height, img_width, num_channels))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(num_classes, activation='softmax'))
# Compile the model
model.compile(optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=['accuracy'])
# Train the model
history = model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test))
# Evaluate the model
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2) print('Test accuracy:', test_acc) ```
Please note that this is a basic template for a CNN model and you may need to customize it further based on the specific requirements of your oncology research project, such as the input image size, number of channels, and the number of classes in your dataset. Additionally, you will need to preprocess your own dataset before training the model.
Sent from my iPhone