donggwan123 / fast-R-cnn

0 stars 0 forks source link

fast R-cnn help #1

Open donggwan123 opened 3 weeks ago

donggwan123 commented 3 weeks ago

i'm used R/Rstudio

donggwan123 commented 3 weeks ago

i want acttivating this code (R, 4.4.0)

use_python("C:/Users/User/anaconda3/envs/donggwan/python.exe", required = TRUE) py_config()

python: C:/Users/User/anaconda3/envs/donggwan/python.exe libpython: C:/Users/User/anaconda3/envs/donggwan/python36.dll pythonhome: C:/Users/User/anaconda3/envs/donggwan version: 3.6.13 |Anaconda, Inc.| (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)] Architecture: 64bit numpy: C:/Users/User/anaconda3/envs/donggwan/Lib/site-packages/numpy numpy_version: 1.19.5 keras: C:\Users\User\AppData\Roaming\Python\Python36\site-packages\keras__init__.p

Define input shapes

input_shape <- c(224, 224, 3) num_classes <- 20 # 클래스 수

base_model <- keras::application_resnet50( include_top = FALSE, weights = "imagenet", input_shape = input_shape )

Define input shapes

input_shape <- c(960, 1280, 3) num_classes <- 20 # Set the number of classes num_rois <- 32 # Number of ROIs to consider

Load the base ResNet50 model

base_model <- keras::application_resnet50( include_top = FALSE, weights = "imagenet", input_shape = input_shape )

Define the ROI Pooling layer

roi_pooling <- function(feature_map, rois, pool_size) {

This function simulates a simple ROI Pooling operation.

pooled_features <- lapply(1:nrow(rois), function(i) { roi <- rois[i, ] x1 <- as.integer(roi[1]) y1 <- as.integer(roi[2]) x2 <- as.integer(roi[3]) y2 <- as.integer(roi[4]) cropped <- feature_map[,,y1:y2, x1:x2] pooled <- layer_average_pooling_2d(pool_size = pool_size)(cropped) pooled }) do.call(abind::abind, c(pooled_features, along = 1)) }

Define inputs

input_image <- layer_input(shape = input_shape) input_rois <- layer_input(shape = c(num_rois, 4))

Feature map from the base model

feature_map <- base_model(input_image)

Define a lambda function to wrap roi_pooling

roi_pool_lambda <- function(x) { roi_pooling(x, input_rois, c(7, 7)) }

Apply ROI Pooling using layer_lambda

pooled_features <- layer_lambda(function(x) roi_pool_lambda(x), name = "roi_pool_lambda")(list(input_image))

Classification and regression heads

flattened <- layer_flatten()(pooled_features) dense_1 <- layer_dense(units = 1024, activation = "relu")(flattened)

classifier <- layer_dense(units = num_classes, activation = "softmax")(dense_1) bbox_regressor <- layer_dense(units = 4)(dense_1)

Define the model

model <- keras_model(inputs = list(input_image, input_rois), outputs = list(classifier, bbox_regressor))

Compile the model

model %>% compile( optimizer = optimizer_adam(), loss = list("categorical_crossentropy", "mse"), metrics = list("accuracy") )

Model summary

summary(model)

Example data (replace with actual data)

num_samples <- 100 # Number of samples train_images <- array(runif(num_samples 960 1280 3), dim = c(num_samples, 960, 1280, 3)) train_rois <- array(runif(num_samples num_rois 4), dim = c(num_samples, num_rois, 4)) train_labels <- matrix(runif(num_samples num_classes), nrow = num_samples, ncol = num_classes) train_bboxes <- matrix(runif(num_samples * 4), nrow = num_samples, ncol = 4)

Train the model

history <- model %>% fit( x = list(train_images, train_rois), y = list(train_labels, train_bboxes), epochs = 10, batch_size = 32, validation_split = 0.2 )

donggwan123 commented 3 weeks ago

this is my matter

pooled_features <- layer_lambda(function(x) roi_pool_lambda(x), name = "roi_pool_lambda")(list(input_image)) py_call_impl(callable, call_args$unnamed, call_args$named)에서 다음과 같은 에러가 발생했습니다: TypeError: init() missing 1 required positional argument: 'function' Run reticulate::py_last_error() for details. reticulate::py_last_error()

── Python Exception Message ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Traceback (most recent call last): File "C:\Users\User\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\training\tracking\base.py", line 530, in _method_wrapper result = method(self, *args, **kwargs) TypeError: init() missing 1 required positional argument: 'function'

── R Traceback ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ▆

  1. └─keras3::layer_lambda(function(x) roi_pool_lambda(x), name = "roi_pool_lambda")
  2. └─keras3:::create_layer(keras$layers$Lambda, object, args)
  3. ├─base::do.call(LayerClass, args)
  4. └─reticulate (local) <python.builtin.type>(name = "roi_pool_lambda")
  5. └─reticulate:::py_call_impl(callable, call_args$unnamed, call_args$named) See reticulate::py_last_error()$r_trace$full_call for more details.
donggwan123 commented 3 weeks ago

my version is

tf_version() [1] ‘2.6’ installed.packages()["keras", "Version"] [1] "2.15.0"