Different prediction based on way of loading image

So I have a mask classification task to predict if person is wearing a mask or not and it was trained using this way of image loading

def preprocess_image(image_path, label):
    print('Preprocessing now')
    # Read and decode the image
    img = tf.io.read_file(image_path)
    img = tf.image.decode_jpeg(img, channels=3)
    # Resize and rescale the image
    img = tf.image.resize(img, (224, 224))
    img = tf.image.convert_image_dtype(img, tf.float32)
    return img, label


def create_dataset(dataframe, batch_size, label_encoder):
    
    # Create a dataset from dataframe
    image_paths = dataframe['Image_Path'].values
    labels = dataframe['Label'].values
    labels = label_encoder.transform(labels)
    
    dataset = tf.data.Dataset.from_tensor_slices((image_paths, labels))
    # Map preprocessing function in parallel
    print('entering mapping')
    dataset = dataset.map(preprocess_image, num_parallel_calls=tf.data.experimental.AUTOTUNE)
    # Shuffle and batch the dataset
    print('leaving mapping entering shuffle')

    dataset = dataset.shuffle(buffer_size=(6000))
    print('leaving shuffle entering batching')

    dataset = dataset.batch(batch_size)
    print('leaving batching entering prefetching')

    # Prefetch data for better GPU utilization
    dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
    return dataset

# Example usage:
batch_size = 256
train_dataset1 = create_dataset(TrainData, batch_size, label_encoder)
validation_dataset1 = create_dataset(ValidationData, batch_size, label_encoder)
test_dataset1 = create_dataset(TestData, batch_size, label_encoder)

it gets 99% accuracy and validation but when running the model for inference using .predict() and loading test images using the same preprocessing code it returns wrong perdiction to reallife images and even images from the training and validation set itself….but when loading images using this code the predictions work perfectly

def preprocess_image(image_path):
    print('Preprocessing now')
    # Open the image using PIL
    img = Image.open(image_path)
    # Resize the image
    img = img.resize((224, 224))
    # Convert the image to a NumPy array
    img_array = tf.keras.preprocessing.image.img_to_array(img)
    
    # Expand dimensions to match the expected input shape of the model (batch_size, height, width, channels)
    img_array = tf.expand_dims(img_array, axis=0)
    # Preprocess the image (normalize, etc.)
    
    return img_array

so could someone explain the change ? as this problem is also affecting me in another project which is emotion detection (no matter the loaded image the prediction is the same but small change in decimals when ran on python notebook or when running inference on mobile application flutter)

As i said above i tried different loading of image method and it worked flawlessly i want to understand where this problem stems from and how can it be avoided to make it work also on flutter mobile application

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật