Computer Vision News - May 2023
14 Computer Vision Code # X: (num_samples, height, width, channels) # y: (num_samples, n_classes) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Create the ResNet model input_shape = (224, 224, 3) # Replace with your input shape n_classes = 4 # Replace with your number of classes (e.g., 4 for red blood cells, white blood cells, platelets, and background) model = build_resnet(input_shape, n_classes) # Compile the model optimizer = Adam(lr=1e-4) model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) # Data augmentation train_datagen = ImageDataGenerator( rescale=1./255, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest') test_datagen = ImageDataGenerator(rescale=1./255) # Train the model batch_size = 32 epochs = 20 model.fit(train_datagen.flow(X_train, y_train, batch_size=batch_size), steps_per_epoch=len(X_train) // batch_size, epochs=epochs, validation_data=test_datagen.flow(X_test, y_test, batch_size=batch_size), validation_steps=len(X_test) // batch_size) # Evaluate the model scores = model.evaluate(test_datagen.flow(X_test, y_test, batch_size=batch_size)) print("Test loss:", scores[0]) print("Test accuracy:", scores[1])
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=