<jupyter_start><jupyter_text>Feature extraction function with VGG16<jupyter_code>datagen <- image_data_generator(rescale = 1/255)

extract_features <- function(dataframe, sample_count){
    features <- array(0, dim = c(sample_count, 4,4,512))
    labels <- matrix(0,nrow = sample_count,nrow(unique(dataframe[,3])))
    
    generator <- flow_images_from_dataframe(
    dataframe = dataframe, 
    x_col = "file_path", 
    y_col= "landmark_id",
    generator = datagen,
    target_size = c(150,150),
    batch_size = batch_size,
    class_mode="categorical"
    )
    
    i <- 0
    while (TRUE) {
        batch <- generator_next(generator)
        inputs_batch <- batch[[1]]
        labels_batch <- batch[[2]]
        
        features_batch <- conv_base %>% predict(inputs_batch)
        
        index_range <- ((i * batch_size)+1) : ((i+1)*batch_size)
        
        features[index_range,,,] <- features_batch
        labels[index_range,] <- labels_batch
        
        i <- i+1
        print(i)
        
        if (i*batch_size >= sample_count)
            break
    }
    
    list(
    features = features,
    labels = labels)
}
train_dataframe <- read_csv(file = "/kaggle/input/1-read-landmark-recognition-train-test-validation/train_dataframe.csv")
train_dataframe$landmark_id <- as.character(train_dataframe$landmark_id)
train_dataframe$count_class <- as.character(train_dataframe$count_class)
head(train_dataframe)
dim(train_dataframe)
train <-extract_features(dataframe = train_dataframe,sample_count = nrow(train_dataframe))
str(train)<jupyter_output>List of 2
 $ features: num [1:11712, 1:4, 1:4, 1:512] 0 0.0815 0 0 0.1299 ...
 $ labels  : num [1:11712, 1:7] 0 0 0 0 0 0 0 0 0 0 ...<jupyter_text>Reshape to the last layer<jupyter_code># reshape
reshape_features <- function(features){
    array_reshape(features, dim = c(nrow(features), 4*4*512)) 
}
train$features <- reshape_features(train$features)
str(train)
saveRDS(object = train,file = "train.Rds")<jupyter_output><empty_output><jupyter_text>Class indexing<jupyter_code># label index
dataframe=train_dataframe
datagen <- image_data_generator(rescale = 1/255)
generator <- flow_images_from_dataframe(
    dataframe = dataframe, 
    x_col = "file_path", 
    y_col= "landmark_id",
    generator = datagen,
    target_size = c(150,150),
    batch_size = batch_size,
    class_mode="categorical"
    )    
label_map = (generator$class_indices)
col_names=names(label_map)
col_names[1:5]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12449799196787148, 'fraction_numerical': 0.03614457831325301, 'mean_word_length': 3.75381679389313, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '420569', 'license': 'MIT', 'n_tokens_mistral': 955, 'n_tokens_neox': 867, 'n_words': 151, 'path': 'vgg16-and-train-generator-function-in-r.ipynb', 'repo_name': 'GiorgosAnastasakos/googleLandMarkRec_kaggle', 'size': '2490'}