Views
No views yet
! pip install --quiet autogluon.multimodal1import pandas as pd
2import re
3
4def preprocess_text(text):
5
6 # Remove URLs
7 text = re.sub(r"http\S+", "", text)
8
9 # Remove mentions (e.g., @username)
10 text = re.sub(r"@\S+", "", text)
11
12 # Remove remaining @ symbols
13 text = re.sub(r"@", "", text)
14
15 # Remove ".com" at the end of the text
16 text = re.sub(r".com$", "", text)
17
18 # Remove emojis using a regular expression pattern
19 emoji_pattern = re.compile("[" u"\U0001F600-\U0001F64F" u"\U0001F300-\U0001F5FF"
20 u"\U0001F680-\U0001F6FF" u"\U0001F1E0-\U0001F1FF" "]+", flags=re.UNICODE)
21 preprocessed_text = emoji_pattern.sub(r'', text)
22
23 return preprocessed_text
24
25stB_test = pd.read_csv("data/test/stB_test.csv")
26
27test_data = pd.DataFrame(columns=['image_path', 'text'])
28count = 1
29
30for column, row in stB_test.iterrows():
31 sample_image = row['filename'].replace("/content/drive/MyDrive/CASE2023_Task4/CASE2023_TASK4_TestData/subtaskB/","")
32 sample_image = '/content/data/test/subtaskB/'+ sample_image
33 sample_text = row['text']
34 sample_text = preprocess_text(sample_text)
35 test_data.loc[count] = [sample_image, sample_text]
36 count+=11import os
2import json
3import numpy as np
4import warnings
5from autogluon.multimodal import MultiModalPredictor
6warnings.filterwarnings('ignore')
7np.random.seed(0)
8
9model_path = 'model.ckpt'
10predictor = MultiModalPredictor.load(model_path)
11predictions = predictor.predict(test_data)
12
13# Get index number from test set
14id = []
15for column, row in stB_test.iterrows():
16 sample_image = row['filename'].replace("/content/drive/MyDrive/CASE2023_Task4/CASE2023_TASK4_TestData/subtaskB/","")
17 sample_id = int(sample_image.replace(".jpg",""))
18 id.append(sample_id)
19
20# Save the prediction results
21prediction = predictions.tolist()
22results = []
23for x, y in zip(id, prediction):
24 line = '{"index": '+ str(x)+ ', "prediction": '+ str(y)+ '}'
25 results.append(line)
26
27# Export the DataFrame to a JSON file
28model_name = 'subtaskB'
29file_path = f"/content/drive/MyDrive/CASE2024/subtaskB/results/{model_name}-submission.json"
30
31with open(file_path, 'w') as json_file:
32 for i in range(len(results) - 1):
33 json_file.write(results[i])
34 json_file.write('\n')
35 json_file.write(results[-1])